本文介绍了为什么我重写规则,不能重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写的URL像

  site.com/eng?sentence=a-sentence
 

要像:

  site.com/eng/a-sentence
 

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

 的RewriteCond%{THE_REQUEST} ^ GET \ S / + ENG一句=([^ /&放大器;?\ S] +)?[NC]
重写规则^ / ENG /%1 [NC,L,NE]
 

我拨弄着它,相比其他重写规则,我发现了一个很长一段时间,但都无法得到它的工作,我实在想不通为什么。我想从我的htaccess每次我测试消除所有其他重写。

这是我的htaccess文件:

 #使用PHP5.3单php.ini中的默认
AddHandler的应用程序/ x-的httpd-php54s的.php
将AddType应用/ X的httpd  -  PHP的.htm的.html

选项​​-Indexes -MultiViews

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

RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME} \ PHP -f
重写规则^(。*)$ $ 1.PHP [L]

的RewriteCond%{THE_REQUEST} ^ GET \ S / + ENG一句=([^ /&放大器;?\ S] +)?[NC]
重写规则^ / ENG /%1 [NC,L,NE]


< FilesMatch&GT\(JPG | JPEG | PNG | | GIF SWF)$。;
    页眉设置缓存控制最大年龄= 604800,公开
< / FilesMatch>
 

preferably,如果你能解决我的规则,而不是写你自己一个人,我将不太可能明白了,那将是巨大的。

解决方案

要更改URL在浏览器中,你需要使用研究标记外部重定向。

还有是特殊的正则表达式的符号,并匹配一个你需要逃避它像 \ 。你也需要一个目标URI的结尾去掉查询字符串。

您完整的.htaccess:

  RewriteEngine叙述上

#实际Oneto酒店pretty的URL  - 外部重定向
的RewriteCond%{THE_REQUEST} \ S / + ENG \ .PHP \句子=([^&放大器; \ S] +)?[NC]
重写规则^ / ENG /%1? [L,NE,R = 302]

#pretty的URL,以实际产品 - 内部重写
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^ ENG /([^ /] +)/ $ /eng.php?sentence=$1 [L,QSA,NC]

#在内部前进/ DIR /富到/dir/foo.php
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{DOCUMENT_ROOT} / $ 1.PHP -f
重写规则^(。+?)/?$ /$1.php [L]
 

我强烈建议您阅读: URL重写简介

I want to rewrite the urls like

site.com/eng?sentence=a-sentence

to be like:

site.com/eng/a-sentence

I only want the text shown in the brower url to change, I dont want a redirect.

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

I have fiddled with it, comparing other rewrite rules i found for a long time, but just cant get it to work, And i really can't figure out why. I tried removing all other rewrite from my htaccess everytime i tested.

here is my htaccess file:

# 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>

preferably, if you could fix my rule rather than write your own one that I won't likely understand, that would be great.

解决方案

To change URL in browser you need external redirect using R flag.

Also ? is special regex symbol and to match a literal ? you need to escape it like \?. Also you would need a ? in the target URI's end to strip off query string.

Your complete .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]

I highly recommend you to read: Apache mod_rewrite Introduction

这篇关于为什么我重写规则,不能重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:47