首页 文章

mod_rewrite重定向www域的问题

提问于
浏览
1

尝试配置.htaccess mod_rewrite规则以将页面http://www.example.com/page1/重定向到http://www.example.com/page2/ .

有 Value 的.htaccess规则是:

RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} ^/?page1/?$
RewriteRule (.*) http://www.example.com/page2/ [R=301,L]

RewriteRule ^([^/]*)/$ index.php?s_page=$1 [L]

如果我发送获取查询,此规则正常:“GET http://example.com/page1/” .

但是,如果我尝试为域www.example.com编写附加规则,例如:

RewriteCond %{HTTP_HOST} ^www.example.com
RewriteCond %{REQUEST_URI} ^/?page1/?$
RewriteRule (.*) http://www.example.com/page2/ [R=301,L]

重定向无法正常工作 . 如果我尝试发送查询:“GET http://www.example.com/page1/ " then, at first, browser sends first query " GET http://www.example.com/page1/ " and server's answer status is " 301永久移动" for this query (and html page is sended by server as answer to the first query). Then, browser sends second query " GET http://www.example.com/page2/”(并且服务器再次发送html页面) . 我需要条件和规则类似于查询域名使用www和查询没有域名的www前缀 .

PS . 对不起英语 .

1 回答

  • 3

    您不需要 www 域的新规则 . 只需调整正则表达式:

    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
    RewriteRule ^page1/?$ http://www.example.com/page2/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ index.php?s_page=$1 [L,QSA]
    

相关问题