mod_rewrite and double slash issue

前端 未结 5 976
半阙折子戏
半阙折子戏 2020-12-28 17:56

I applied the following mod_rewrite rule in Apache2 to redirect from non www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^exa         


        
相关标签:
5条回答
  • 2020-12-28 18:12

    Putting a slash into your pattern should resolve this issue:

    RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
    
    0 讨论(0)
  • 2020-12-28 18:14

    Fixed with:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com$1 [R=301,L]
    

    because $1 by default contains the index path /

    0 讨论(0)
  • 2020-12-28 18:17

    Actually, you will always have double slashes due to

    RewriteRule ^(.*)$ http://www.wxample.com/$1 [R=301,L]
    

    combined with the fact that REQUEST_URI (that you are matching on) normally contains a starting slash. What you can try is RewriteRule ^(.*)$ http://example.com$1, and then send a broken HTTP request GET foo HTTP/1.0 and see if Apache deals with it properly.

    0 讨论(0)
  • 2020-12-28 18:26
    RewriteRule ^\/?(.*)$ http://www.mydomain.com/$1 [R=301,L]
    
    0 讨论(0)
  • 2020-12-28 18:34

    That is because the root path is /, and you are appending whatever you get in RewriteRule (the first case works fine because it doesn't match the condition so no rewrite is performed).

    You can try something like this:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
    # for the home page
    RewriteRule ^/$ http://www.mydomain.com/ [R=301,L]
    # for the rest of pages
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题