.htaccess Rewrite Wordpress Subdirectory without breaking other subdirectories
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, wordpress, mod-rewrite, url-rewriting, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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
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).
Comments
Post a Comment