Sreekanth Sagar Sreekanth Sagar - 1 year ago 85
Apache Configuration Question

Apache Rewrite Rule on Dispatcher

How can I do a rewrite rule for both http://domain/abc to rewite/redirect to http://domain/abc.html but it should work when I have url as http://domain/abc/def.html. Currently when I do

redirect 301 "abc" "abc.hml"
the second url is also redirecting to http://domain/abc.html/def.html.

My current rules.

<IfModule rewrite_module>
RewriteEngine On

RewriteRule ^/$ /content/aaa-123/abc.html [PT,L]
RewriteRule ^/index.html$ /content/aaa-123/abc.html [PT,L]
RedirectMatch ^/abc$ /abc.html

RewriteCond %{REQUEST_URI} !^/(.*)/$
RewriteCond %{REQUEST_URI} !^(.*)\.(.*)$

RewriteRule ^/(.*)$ /content/$1/ [L]

RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]

ErrorDocument 404 /errors/404.html
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"

<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding


Answer Source

Use RedirectMatch instead of Redirect to match using regular expressions:

Options -MultiViews
RewriteEngine On

RewriteRule ^/abc/?$ /abc.html [NC,L,R=302]

RewriteRule \.(css|jpe?g|gif|png|js)$ - [L,NC]

RewriteRule ^/(index\.html)?$ /content/aaa-123/abc.html [PT,L,NC]

RewriteCond %{REQUEST_URI} !^/(.*)/$
RewriteCond %{REQUEST_URI} !^(.*)\.(.*)$
RewriteRule ^/(.*)$ /content/$1/ [L]

Clear your browser cache before testing this rule and do restart Apache after making these changes.