【问题标题】:How to redirect from the www to the non-www version in an HTACCESS file?如何在 HTACCESS 文件中从 www 重定向到非 www 版本?
【发布时间】:2017-01-17 20:57:00
【问题描述】:

我已经在我的 Linux 服务器中安装了 Laravel 5.2 并且我几乎没有通过修改 HTACCESS 文件来做到这一点,所以现在我正在尝试从 www 重定向到非 www 版本,我的意思是就像来自 www 的 stackoverflow .example.com >>TO>> example.com ,但恐怕因为我的服务器可能会崩溃所以请帮助解释代码:

RewriteEngine on 

# I changed my website real name to example
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 

RewriteCond %{REQUEST_URI} !^/example/ 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ /example/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ example/index.php [L]

提前致谢

【问题讨论】:

  • 什么是/example/ 目录?访问 Laravel 主页的完整 URL 是什么?
  • yes "example" 是一个目录,因为 Laravel 项目必须对公共访问隐藏,这是我在教程中被告知要做的事情

标签: php .htaccess redirect no-www


【解决方案1】:

你可以试试这些规则:

RewriteEngine on 

# I changed my website real name to example
RewriteCond %{HTTP_HOST} ^www\.(.+)$ 
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

RewriteRule ^/?$ example/index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(?!example/)(.*)$ example/$1 [L,NC]

【讨论】:

  • 很抱歉直到现在才回复你:),它正在工作,但是当我转到 www.example.com/ex1/ex2/ex3.php 时出现问题,它将我重定向到 example.com/index.php,但我希望它重定向我到example.com/ex1/ex2/ex3.php,我的意思是我想保留参数,很抱歉打扰你^__^
  • 在测试时完全清除浏览器缓存。你有任何其他规则或任何其他 .htaccess 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 2011-04-12
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
相关资源
最近更新 更多