简体   繁体   中英

Mod_rewrite on a dynamic rule not working

I have several web sites and using the same mod_rewrite . I like to add two additional conditions to it and based on many of solutions that I have found on here and none seem to be working.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I have my sites working with the following solution.
domain.com forwards to www.domain.com

I like to add the following two conditions to my conditions above.

  1. Remove trailing slashes. If I have www.domain.com/ it does remove that trailing slash, unfortunately, for some reason if there are multiple slashes at the end it doesn't www.domain.com//

  2. Removing index.html from www.domain.com/index.html

Making this dynamic is what may be through this off. If I use the suggestion on domain itself, I have not problems. It is once I add http://www.%{HTTP_HOST}%{REQUEST_URI} where everything goes wonky.

i use this rewrite for redirecting domain.com to www.domain.com. No problems with //

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R,L]

#this could work for you (untested)
RewriteCond %{HTTP_HOST} !^www.\$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R,L]

#redirect /index.html to /
RewriteRule ^/index.html / [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM