htaccess - 如何捕获当前的重写URL?

时间:2013-12-31 11:17:42

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

我希望通过.htaccess捕获当前的重写网址,以便创建自定义HTTP链接标头。

脚本网址:

http://localhost/index.php

重写的网址:

http://localhost/news

htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)$ index.php?&m=$1 [NC,L,QSA]

RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
RewriteRule ^(.*) - [E=ORIGQRY:%{REQUEST_URI}]

输出:

ORIGPROTO = http
ORIGURI = localhost
ORIGQRY = index.php

我期待ORIGQRY等于' news'

我的问题是如何通过htaccess捕获当前重写的URL?

编辑1

RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} ^(.*)[^/]$
RewriteRule . %1/ [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)_(.*)$
RewriteRule . %1-%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\º(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\!(.*)$
RewriteRule . %1 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\,(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\'(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\"(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\“(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\”(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\((.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\)(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\[(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\](.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\{(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\}(.*)$
RewriteRule . %1%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\:(.*)$
RewriteRule . %1-%2 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\+(.*)$
RewriteRule . %1-%2 [R=301,L]

#avoid uppercase & accented

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)(A|Â|â|Æ|æ|À|à|Å|å|Ã|ã|Ä|ä)(.*?)$
RewriteRule . %1a%3 [R=301,L]

.
.
.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ index.php?&m=$1 [NC,QSA,E=ORIGQRY:$1,L]

#404 page

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pt=404&acc=404_page [L]
Header always set Link "<http://cache_api.com/%{ORIGPROTO}e://%{ORIGURI}e%{ORIGQRY}e>;rel=timegate"

1 个答案:

答案 0 :(得分:1)

规则按照它们在.htaccess中的存在顺序执行。您的第一条规则就是将REQUEST_URI修改为/index.php

FIX:更改规则的顺序:

RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
RewriteCond %{THE_REQUEST} \s/+([^\s?]+)
RewriteRule ^ - [E=ORIGQRY:%1]

Header always set Link "<http://cache_api.com/%{ORIGPROTO}e://%{ORIGURI}e/%{ORIGQRY}e>;rel=timegate"

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ index.php?m=$1 [NC,QSA,L]