简体   繁体   English

htaccess中的重写规则

[英]rewrite rule in htaccess

I have a website structured: 我的网站结构如下:

- root
  - .htaccess
  - wordpress installation
  - folder xxx
    - wordpress

In my .htaccess I have this rule: 在我的.htaccess文件中,我有以下规则:

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

The problem is that it don't allow me to go in: 问题是它不允许我进入:

www.mywebsite.it/xxx 

because a loop redirection happen. 因为发生了循环重定向。 If I erase the rule, all goes good. 如果我删除规则,一切都会顺利进行。 How can I modify it? 我该如何修改? I want that if user type mywebsite.it/ or mywebsite.it/xxx, it is redirected to www version. 我希望如果用户键入mywebsite.it/或mywebsite.it/xxx,它将被重定向到www版本。

my .htaccess: 我的.htaccess:

    <IfModule mod_rewrite.c>

RewriteEngine On

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

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

Thanks a lot. 非常感谢。

This rule works in isolation but Wordpress stuff is hijacking over it. 该规则是孤立运行的,但Wordpress的东西正在劫持它。 Slightly modified rule: 略有修改的规则:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  1. Make sure this is 1st rule after RewriteEngine On line in DOCUMENT_ROOT/.htaccess 确保这是 DOCUMENT_ROOT/.htaccess RewriteEngine On行之后的第一条规则
  2. make sure in WP permalink structure you have site address with www. 确保在WP permalink structure您的网站地址带有www. otherwise WP will again send it back to non-www 否则WP会再次将其发送回非www
  3. If you have any .htaccess below root then they should this additional line below RewriteEngine On line: 如果您在root之下有任何.htaccess ,那么他们应该在RewriteEngine On之下的这行附加代码RewriteEngine On线:

     RewriteOptions Inherit 

Also test in a different browser to avoid 301 caching. 还要在其他浏览器中进行测试,以避免301缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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