我想使用htaccess和mod_rewrite制作两种具有这些风格的URL。
http://www.site.com/product
http://www.site.com/product/1/something
为此,我在htaccess文件中添加了以下几行:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)$ /index.php?p=$1 [F]
但是每当我运行应用程序并提示http://localhost/project/,web服务器将我重定向到http://localhost/。
现在我有两个问题:
- 为什么我重定向到localhost,为什么上面的代码不起作用
- 如何在干净URL的末尾添加尾部斜杠
-
正在添加重写数据库/应该有助于
-
将规则更改为
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [N] RewriteRule ^([^/]*)/?$ /index.php?p=$1 [F]
最后的.htaccess应该是这样的:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
您可能想要去掉N
和F
标志。不知道你为什么会被重定向,看起来你的规则并不是对此负责的。您还需要重复两种规则的2个条件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L,QSA]