Search code examples
.htaccessmod-rewrite

What's wrong with this mod_rewrite?


RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/errors/$
RewriteCond %{REQUEST_URI} !^/content/$
RewriteRule ^(.*)$ /domain/$1/ [L]

The first and last line work, they ensure that all content from that domain goto that subdirectory. The two in the middle I am attempting to keep from redirecting. I need to access those 2 folders from the subdirectory, but the HTTP_HOST just sends it back to the same folder (error 404).

I want it to ignore that rule if it's looking for domain.com/content/(.*) or domain.com/content/(.*). Please note that I do not have access to server config, and .htaccess is the only way I can do this.


Solution

  • I would suggest compacting your .htaccess code like this:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/*(domain|errors|content)/ [NC]
    RewriteRule . /domain%{REQUEST_URI}/ [L]
    
    • . needs to be escaped as \.
    • [NC] is for ignore case comparison
    • Negative URI match for errors and content can be combined in single rule with OR |
    • %{REQUEST_URI} already has your original URI so no need to capture that
    • Added domain in negative match to avoid infinite looping