【发布时间】:2014-06-13 12:05:07
【问题描述】:
我会通过使用以下 .htaccess 的查询字符串将 域、子域和请求 uri 传递给 php 文件来重写 url
# Apache configuration
Options -Indexes
Options -Multiviews
Options +FollowSymLinks
# Rewrite engine configuration
RewriteEngine on
RewriteBase /
# Sub domain redirection (301 redirect)
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.0-9]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Request redirection
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.[^\.0-9]+$
RewriteRule ^(.*)$ index.php?app=%2&sub=%1&req=$1 [QSA,END]
以下是一些工作示例:
http://dev.testdomain.tld/hello/world
=> /index.php?app=testdomain&sub=dev&req=/hello/world
和
http://www.example.tld/?var=test
=> /index.php?app=example&sub=www&req=/&var=test
但如果我有这样的事情:
http://dev.testdomain.tld/hello/world?app=FAIL&sub=FAIL&req=FAIL
=> /index.php?app=testdomain&sub=dev&req=/hello/world&app=FAIL&sub=FAIL&req=FAIL
查询字符串将用FAIL(或url查询字符串中的所有其他内容)替换域、子域和请求uri变量。
我可以停用[QSA] 标志,但我想保留原始查询字符串,并防止app, sub, req 变量重写。
【问题讨论】:
标签: php apache .htaccess mod-rewrite