1

How can I do a rewrite rule for both http://example.com/abc to rewrite/redirect to http://example.com/abc.html but it should work when I have the URL as http://example.com/abc/def.html. Currently when I do redirect 301 "abc" "abc.hml", the second URL is also redirecting to http://example.com/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>

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

2 Answers 2

1

Currently when I do redirect 301 "abc" "abc.hml", the second URL is also redirecting to http://example.com/abc.html/def.html.

The mod_alias Redirect directive is prefix matching, which explains your undesirable redirect.

But also, don't mix redirects from both mod_alias (ie. RedirectMatch and Redirect) and mod_rewrite (ie. RewriteRule). Since they are different modules, they execute at different times (usually mod_rewrite first) regardless of the apparent order, so you can end up with confusing conflicts.

Try the following in your server config, to internally rewrite /abc to /abc.html

RewriteRule ^/abc$ /abc.html [L]

However, you will have problems if /abc is a physical directory on the file system since mod_dir will normally try to "fix" the URL by appending a slash. So, you would need to make the trailing slash optional:

RewriteRule ^/abc/?$ /abc.html [L]

Also, disable MultiViews if it is enabled already:

Options -MultiViews

MultiViews (part of mod_negotiation) does the same sort of thing (internally rewrite /abc to /abc.html or /abc.php or whatever it finds) but will execute before mod_rewrite gets a chance.

1
  • Since your question was tagged .htaccess, I initially answered with respect to per-directory rewrites in .htaccess. It seems these directives are directly in the server config - I've updated my answer. The RewriteRule patterns will need a slash prefix when used in the server config. In .htaccess, the directory-prefix is first removed from the URL-path that is matched, so the pattern does not start with a slash.
    – MrWhite
    Aug 25, 2016 at 19:27
0

This worked for me. Some one helped me in the site with this.

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]
2
  • Are you sure you want an external redirect, not an internal rewrite (as in your other directives and in my answer)? The NC flag is also unnecessary here - in fact, this could result in duplicate content issues.
    – MrWhite
    Aug 25, 2016 at 19:36
  • It to work apache with AEM dispatcher. Aug 26, 2016 at 12:45

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.