htaccess - 将一个规则重写为另一个规则(虚拟文件夹)

时间:2017-01-27 12:28:57

标签: apache .htaccess mod-rewrite url-rewriting

我在根目录中有一个名为propertyid.php

的文件

我有RerwiteRule可以正常工作: RewriteRule for-sale/(.*) propertyid.php?pid=$1

用户输入此URL并在地址栏中显示相同的URL domain.com/for-sale/(pid)

我想要第二条允许用户输入此网址的规则 domain.com/sell/(pid)

但我希望第一个URL显示在地址栏中 domain.com/for-sale/(pid)

我尝试了以下

的多种变体
RewriteRule ^sell$ /for-sale/propertyid.php?pid=$1
RewriteRule sell/(.*) for-sale?pid=$1

1 个答案:

答案 0 :(得分:0)

您需要将/sell/(.*)重定向到/for-sale/(.*)。您的/for-sale/(.*)propertyid.php?pid=$1的重定向规则会处理剩下的事情。 只需检查几件事:

  1. /sell/(.*)/for-sale/(.*)规则应该超越 /for-sale/(.*)propertyid.php?pid=$1重定向规则应该 返回301 http代码永久移动。

  2. 检查多个重定向。

  3. 你的.htaccess应该是这样的:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^sell(.*)$  http://%{HTTP_HOST}/for-sale$1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^for-sale/(.*)$  propertyid.php?pid=$1 [L]