0
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 中,我有一些包含 href 的代码,如下所示:

/people/john--> 当一个人点击这个时,它应该转到:people.php?q=john 但是,当一个人点击这个时,我的 .htaccess 没有重定向/people/john。顺便说一句,即使那是幕后发生的事情,网址也应该始终说“/people/john不”。people.php?q=john

只想说,我有

/city/newyork --> city.php?q=newyork
/country/australia --> country.php?q=australia 

我一直在努力和在这里寻找确切的位置,但仍然没有运气。我感谢所有的回应。

4

4 回答 4

1

根据您的示例,您的网址如下所示:

/city/newyork, where city is the php file to call with parameter newyork

如果这就是你想要的,试试这个:Options +FollowSymLinks RewriteEngine on

# 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
于 2013-04-04T20:38:28.083 回答
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]
于 2013-04-04T20:36:29.153 回答
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(因此,如果您在子文件夹中,它可能不是/适合您)
-如果url不是现有文件
-并且url不是现有文件dir
- 将参数映射到 files.php

刚刚意识到您正在使用 Files.php ......奇怪,如果它是所有内容都应该通过的路由,则应该将其称为索引以保持一致性,或者创建一个将文件夹/文件/*路由到 index.php?tab=files& 的索引。 ..

希望这可以帮助

于 2013-04-04T20:46:26.160 回答
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"
  }
于 2013-04-06T14:54:46.817 回答