1

我的目标是实现以下目标:

我的查询: properties.php?id=1234-N-StreetName-etc

变成:

properties/1234-N-Beverly-Blvd.html

properties.php将根据 mysql 数据库评估$id(In this case ) 的值。any-address.html如果某个值不在数据库中,则文件 properties.php 会将用户重定向到 404 页面。 $id 的值可以是字母字符、破折号“-”或数字的任意组合。

我的代码不起作用:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^properties/([a-zA-Z0-9_-]+)\.html$ properties.php?id=$1

你能帮我获得正确的 htaccess 重写代码吗?

PS。我对.htaccess 一无所知。我从示例网站修改了此代码。

4

1 回答 1

1

您将需要 L 标志将该规则标记为Last和 QSA 用于Query String Append

在目录下的 .htaccess 中使用此代码DOCUMENT_ROOT

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^properties/([^.]+)\.html$ /properties.php?id=$1 [L,NC,QSA]

要在子目录 sun 中应用此规则,请使用以下代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /sub

RewriteRule ^properties/([^.]+)\.html$ /sub/properties.php?id=$1 [L,NC,QSA]

# translate html -> php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.html$ /$1.php [L,NC]

这将处理诸如 URI/sub/properties/abc-123.html并将其转发到/sub/properties.php?id=abc-123

于 2013-03-14T20:55:59.307 回答