2013-10-12 43 views
1

我想重写这些URL一样为什么我的重写规则不能重写?

site.com/eng?sentence=a-sentence 

是这样的:

site.com/eng/a-sentence 

我只想在布劳尔URL改变,我不想重定向显示的文字。

RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC] 
RewriteRule^/eng/%1 [NC,L,NE] 

我已经摆弄它,比较其他重写规则,我发现很长一段时间,但都无法得到它的工作,我实在想不通为什么。每次我测试时,我都尝试从我的htaccess中删除所有其他重写。

这里是我的htaccess文件:

# Use PHP5.3 Single php.ini as default 
AddHandler application/x-httpd-php54s .php 
AddType application/x-httpd-php .htm .html 

Options -Indexes -MultiViews 

ErrorDocument 404 /404.php 
ErrorDocument 500 /500.php 

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC] 
RewriteRule^/eng/%1 [NC,L,NE] 


<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> 
    Header set Cache-Control "max-age=604800, public" 
</FilesMatch> 

最好,如果你能解决我的规则,而不是写自己的一个,我不明白可能,这将是巨大的。

回答

0

我只希望显示在浏览器url中的文本发生变化,我不想要 重定向。

要在浏览器中更改URL,您需要使用R标志的外部重定向。

?是特殊的正则表达式符号和匹配一个文字?你需要像\?转义它。你也需要在目标URI的末尾有一个?去掉查询字符串。

你完整的.htaccess:

RewriteEngine On 

# actual oneto pretty URL - external redirect 
RewriteCond %{THE_REQUEST} \s/+eng\.php\?sentence=([^&\s]+) [NC] 
RewriteRule^/eng/%1? [L,NE,R=302] 

# pretty URL to actual one - internal rewrite 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^eng/([^/]+)/?$ /eng.php?sentence=$1 [L,QSA,NC] 

# To internally forward /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L] 

我强烈建议您阅读:Apache mod_rewrite Introduction

+0

我很想相信你,但该规则给了我一个500错误。即使我从我的htaccess中删除所有其他重写规则。 – user1397417

+0

那么隔离的规则绝对有效并重定向到'/ localhost/eng/a-sentence'。但是,责任在于清理你的系统,并确保没有其他.htaccess任何地方没有其他重写规则。报告500是不够的,你需要阅读你的apache error.log并找出真正的错误信息。 – anubhava

+0

现在检查编辑完成.htaccess。请停止创建新问题,并参加此最终决议。 – anubhava