如何显示用户定义的(漂亮)URL

时间:2015-01-26 21:19:55

标签: java php html apache

我在网站上创建了一个文本链接。

  1. 当用户点击该链接时,会将他带到页面:www.example.com/toronto_streets.php 但是,我希望网址显示为www.example.com/toronto.on/

  2. 然后当用户从此页面选择一条街道时,网址将为 www.example.com/street_landing_page.php?street=Option 但是,我希望网址显示为www.example.com/toronto.on/house-for-sale-option

  3. 我尝试编辑.htacccess文件但是没有用......

    以下是我添加到 .htaccess 文件中的内容:

    RewriteEngine on
    
    RewriteRule  ^toronto.on/house-for-sale-condo-mls/?$   house-for-sale-condo-mls.php  [NC]  #...
    

    以上一行是否会将www.example.com/toronto.on/house-for-sale-condo-mls/重定向到www.example.com/house-for-sale-condo-mls.php

    当我调用网址www.example.com/toronto.on/house-for-sale-condo-mls/时,我收到此错误:

      

    找不到请求的网址/toronto.on/house-for-sale-condo-mls   在这台服务器上。


    更新

    这是我当前的.htaccess文件。它将www.example.com/toronto.on重定向到名为cate.php的页面,其中city value ='toronto',省='on'。

    Options +FollowSymLinks 
    RewriteEngine on 
    
    RewriteCond %{HTTP_HOST} ^example\.com 
    RewriteRule ^(.*)$ example.com/$1 [R=permanent,L] 
    

2 个答案:

答案 0 :(得分:0)

在.htaccess中,您将使用一组重写规则,例如:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^toronto.on(/*)$ /toronto_streets.php [L]
RewriteRule ^toronto.on/house-for-sale-option /street_landing_page.php?street=option [L]

您可以使用此网站来帮助您测试重写规则:click here

但是,正如其他人所指出的那样,URL栏中显示的内容将是页面请求的href。因此,应修改页面html上的链接以使用友好的URL(例如/toronto.on/house-for-sale-option,而不是后端url /street_landing_page.php?street=option

答案 1 :(得分:0)

1)您在问题中说明的重写规则中有2个错误:

RewriteEngine on
RewriteRule ^/house-for-sale-condo-mls.php?$ /toronto/house-for-sale-condo-mls/ [QSA,NC,L]

一个在你的头脑中:**你需要拥有你的友好"您发送给客户端的任何html中嵌入的网址。 ** .htaccess对漂亮的URL没有坏处,它使得漂亮的URL内部重定向到真实站点。 这意味着您网站上的链接需要包含:

<a href="/house-for-sale-condo-mls.php">The Text</a>

第二个是缺少反斜杠。

你的问题的第1点也一样:

RewriteRule ^/toronto.on /toronto_streets.php [QSA,NC,L]

我可能会把这样的另一个重写规则放在最重要的规则之前:

 RewriteRule ^/toronto_streets.php /toronto.on  [R=301,L]

如果某个链接没有变更(外部资源),则会将用户重定向到漂亮的网址。

相关问题