1

So I have WordPress installed in /wp/ but I want all WordPress URLs to be rewritten to appear in the root directory. I currently have this working (see below) but I want to change my .htaccess so it doesn't break other subdirectories.

For example, if I want to visit www.example.com/images/image.png, it currently reroutes me to a 404 page on WordPress.

Is this even possible with .htaccess?

Current code:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]

Update: Tested MrWhite's suggestion with Incognito mode and a second browser. No effect. New .htaccess file:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]
# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
0

You need to add a condition to exclude existing files, otherwise a request for /images/image.png will naturally be rewritten to /wp/images/image.png and trigger a 404 in WordPress (unless /wp/images/image.png did actually exist).

For example, add the following condition to your existing rule:

# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f

UPDATE: this did not seem to work. Still goes to the WordPress 404 page

You've put the RewriteCond directive in the wrong place.

RewriteCond directives are not entirely separate statements, they are conditions that apply to the RewriteRule directive that follows. By themselves, RewriteCond directives don't do anything.

The RewriteCond directive needs to go (anywhere) before the RewriteRule directive:

RewriteEngine on

#Wordpress
# Rewrites all URLS without wp in them
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrites all URLS [Replace "example" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.

# Request does not map to an existing file
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /wp/$1 [L]

Also, have a look at the "WordPress" .htaccess file in the /wp subdirectory for reference. That does the same sort of thing.

Aside: Unless you have multiple domains or subdomains that you don't want rewritten then you can remove the hostname check (ie. the RewriteCond directive that checks against the HTTP_HOST server variable).

| improve this answer | |
  • Unfortunately, this did not seem to work. Still goes to the WordPress 404 page. – Quinnell Aug 21 at 6:10
  • Please post the updated contents of your .htaccess file in your question, Make sure you've cleared your browser cache. – MrWhite Aug 21 at 9:49
  • I have added the modified .htaccesscode to the question. Tested it with Incognito mode. – Quinnell Aug 21 at 14:56
  • You've put the RewriteCond directive in the wrong place. I've updated my answer. – MrWhite Aug 21 at 15:32
  • 1
    Ahh, thank you for the explanation. This worked. Thanks so much. – Quinnell Aug 21 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.