【问题标题】:Some Problem with htaccess!htaccess 的一些问题!
【发布时间】:2010-11-25 09:07:11
【问题描述】:
如何将我的用户从 example.com / 或 www.example.com 重定向到 new.example.com
但我不想重定向一些特定的网址,例如:
www.example.com/test.php
api.example.com/testo.php
www.example.com/forum/index.php
我做到了:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
但是 url 的规则是什么?
【问题讨论】:
标签:
apache
.htaccess
http-status-code-301
【解决方案1】:
执行此操作的一种方法是在 .htaccess 文件的前面为仅重定向到自身的特定 URL 创建附加规则(在内部,不使用 3XX 响应),然后使用 L 标志,以便不处理以后的规则对于这些 URL。
例如:
RewriteEngine on
# don't redirect these
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/test.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^api.example.com$
RewriteRule ^(/testo.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/forum/index.php)$ \1 [L]
# redirect these
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
我不确定您的“不重定向”要求是否包括主机名。如果没有,那么只需删除 RewriteCond 行。