当省略末尾的斜杠时,url重写失败

时间:2011-06-24 14:24:48

标签: apache .htaccess mod-rewrite url-rewriting apache2

我要覆盖

http://mysite.com/intranet/symlinks/site.comhttp://mysite.com/site.com

http://mysite.com/intranet/symlinks/site.nethttp://mysite.com/site.net

RewriteCond %{REQUEST_URI} ^(.*\.com|.*\.net)
RewriteRule ^(.*)$ /intranet/symlinks/$1 [L]

它适用于http://mysite.com/site.com/

但如果链接为http://mysite.com/site.com(请注意最后缺少 /

它会带我去http://mysite.com/intranet/symlinks/site.com/

有什么想法吗?

编辑1 :我认为问题在于规则而不是条件,因为我删除了条件并继续遇到同样的问题。我被重定向到http://mysite.com/intranet/symlinks/site.com/

编辑2 这是.htaccess的其余部分,它是CodeIgniter的默认.htaccess:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
   Order Deny,Allow
   Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]

# MY condition rule from above is located here

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

3 个答案:

答案 0 :(得分:1)

更改您的行,以选择性地匹配尾随/

RewriteCond %{REQUEST_URI} ^(.*\.com/?.*|.*\.net/?.*)

答案 1 :(得分:1)

试试这个:(你不需要RewriteCond)

RewriteRule ^([^/]*\.(com|net))$ intranet/symlinks/$1 [L,QSA]

或者如果你想允许一个尾部斜杠:

RewriteRule ^([^/]*\.(com|net))/?$ intranet/symlinks/$1 [L,QSA]

答案 2 :(得分:0)

无论如何我使用

使其工作
RewriteCond %{REQUEST_URI} ^(.*\.com/?.*|.*\.net/?.*)
RewriteRule ^(.*)$ sites/$1/ [L]

注意$1之后第二行末尾的斜杠。

我很确定它不能用于任何其他服务器设置,所以如果你有更好的解决方案,发布它我会接受它

@LazyOne感谢你的帮助