2013-06-27 79 views
1

我试图重写URLURL重写错误

site.com/videos?cat=1&page=2 

这个

site.com/videos/cat-1/page-2 

,并使用此:

RewriteRule ^videos/cat-([0-9]+)/page-([0-9]+)/?$ videos?cat=$1&page=$2 
RewriteRule ^videos/cat-([0-9]+)/?$ videos?cat=$1 

我得到错误#500(内部服务器错误)。什么可能是错误的?

回答

3

最有可能你在Apache中的mod_rewrite引起无限循环并且这是造成500

这种替换代码:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# to redirect /videos?cat=1&page=2 => /videos/cat-1/page-2 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(videos)\?cat=([^&]+)&page=([^\s]+) [NC] 
RewriteRule^/%1/cat-%2/page-%3? [R=302,L,NE] 

RewriteCond %{QUERY_STRING} !^.+$ [NC] 
RewriteRule ^videos/cat-([0-9]+)/?$ /videos?cat=$1 [L,QSA,NC] 

# to forward /videos/cat-1/page-2 => /videos?cat=1&page=2 
RewriteCond %{QUERY_STRING} !^.+$ [NC] 
RewriteRule ^videos/cat-([0-9]+)/page-([0-9]+)/?$ /videos?cat=$1&page=$2 [L,QSA,NC] 
+0

谢谢,那实际上工作。我不明白一些参数,但我会在网上搜索它们。 –

+0

是的,它应该工作,任何问题请留下评论。为了指导mod_rewrite的使用:http://httpd.apache.org/docs/2.2/rewrite/ – anubhava

+0

其实现在这个return''找不到对象!''有什么想法为什么? –

3

这看起来是正确的,但你想做什么实际上是倒过来 - 你试图改写输入网址:

site.com/videos/cat-1/page-2 

要:

site.com/videos?cat=1&page=2 

编辑 你已经编写了规则重定向从videos/cat-1/page-2videos?cat=1&page=2。虽然这是你的选择,但如果你正在为搜索引擎优化或更清洁的网址撰写规则,那么你希望人们输入的网址是videos/cat-1/page-2 - 在这种情况下,你拥有的规则将会正常工作。

如果你想让人们输入messier url,那么@ anubhava的答案对你来说是完美的。

+0

如何在适当的方式进行呢? –

+0

规则没问题 - 您是在浏览器中输入'site.com/videos/cat-1/page-2'还是'site.com/videos?cat = 1&page = 2'? –

+0

'site.com/videos?cat = 1&page = 2'像这样 –