Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]
AddHandler application/x-httpd-php5 .html .htm .txt .php
我的.htaccess对所有请求使用主“files.php”。 在我的files.php中,我有一些包含hrefs的代码,如下所示:
/people/john
- >当一个人点击它时,它应该转到:people.php?q=john
但是,当人们点击此/people/john
时,我的.htaccess不会重定向。
顺便说一句,网址应该总是说/people/john
不是people.php?q=john
,即使这就是幕后发生的事情。
只是想说,我有
/city/newyork --> city.php?q=newyork
/country/australia --> country.php?q=australia
我一直在努力寻找这里找到确切的,但仍然没有运气。我很欣赏所有的回复。
答案 0 :(得分:1)
根据你的例子,你有这样的网址:
/city/newyork, where city is the php file to call with parameter newyork
如果这是你想要的,试试这个: 选项+ FollowSymLinks
上的RewriteEngine# Rewrite /city/newyork to city.php?q=newyork
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) /$1.php?q=$2 [L]
# Rewrite all urls without a second / to files.php?q=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]
AddHandler application/x-httpd-php5 .html .htm .txt .php
答案 1 :(得分:0)
你想让它变得动态吗?我的意思是,/$anything1/$anything2
将重新映射到/$anything1.php?=$anything2
:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1.php?q=%2
如果上面的代码不起作用,请尝试此操作:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) http://%{HTTP_HOST}/%1.php?q=%2 [P]
答案 2 :(得分:0)
以下是我对CodeIgniter的使用(支持非常深的a / b / c / d / e / f / g)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ files.php?/$1 [L]
基本上是逐行说明的
- 打开重写引擎
- 使用web_root(如果你在子文件夹中,它可能不适合你)
- 如果网址不是现有文件
- 并且网址不是现有的目录
- 将参数映射到files.php
刚刚意识到你正在使用Files.php ...很奇怪,如果它是所有应该通过的路径,或者制作一个将文件夹/ files / *路由到index.php的索引,那么它应该只是将其称为索引的一致性吗? =文件&安培; ...
希望这有帮助
答案 3 :(得分:0)
Faa,我做了你说的,下面是我的服务器变量,我认为正确的变量应该是下面的“应该是这个:”。
["QUERY_STRING"]=>
string(17) "q=city/newyork"
["REDIRECT_QUERY_STRING"]=>
string(17) "q=city/newyork"
["REDIRECT_STATUS"]=>
string(3) "200"
["REDIRECT_URL"]=>
string(16) "/city/newyork"
["REQUEST_URI"]=>
string(16) "/city/newyork"
["SCRIPT_FILENAME"]=>
string(30) "/home/mywebsite/myfiles.php"
["SCRIPT_NAME"]=>
string(14) "/myfiles.php"
["PHP_SELF"]=>
string(14) "/myfiles.php"
["argv"]=>
array(1) {
[0]=>
string(17) "q=city/newyork"
}
should be this:
["QUERY_STRING"]=>
string(16) "q=newyork"
["REDIRECT_QUERY_STRING"]=>
string(16) "q=newyork"
["REDIRECT_STATUS"]=>
string(3) "200"
["REDIRECT_URL"]=>
string(25) "/city/newyork"
["REQUEST_URI"]=>
string(25) "/city/newyork"
["SCRIPT_FILENAME"]=>
string(30) "/home/mywebsite/city.php"
["SCRIPT_NAME"]=>
string(14) "/city.php"
["PHP_SELF"]=>
string(14) "/city.php"
["argv"]=>
array(1) {
[0]=>
string(16) "q=newyork"
}