【问题标题】:How to display user defined (pretty) URLs如何显示用户定义的(漂亮的)URL
【发布时间】:2015-01-26 21:19:55
【问题描述】:

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

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

  2. 那么当用户从该页面中选择一条街道时,该 url 将是 www.example.com/street_landing_page.php?street=Option 但是,我希望 URL 显示为 www.example.com/toronto.on/house-for-sale-option

我尝试编辑 .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吗?

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

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


更新:

这是我当前的 .htaccess 文件。它将www.example.com/toronto.on 重定向到一个名为cate.php 的页面,城市值='toronto',省='on'。

Options +FollowSymLinks 
RewriteEngine on 

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

【问题讨论】:

  • 你需要做一些关于 Apache 扩展的研究mod_rewrite 那你就做这样的事情;您尝试在 .htaccess 中写入什么内容?
  • 并且需要将您的“友好”网址嵌入到您发送给客户的任何 html 中。如果您发送的 html 包含内部“丑陋”的 url,那么再多的 mod_rewrite 修改也无法修复客户端浏览器中的 url。
  • 我已编辑问题以反映我对 .htaccess 文件所做的操作
  • thnak 你@j_s_stack .so 是 .htaccess 唯一的方法吗? php中还有其他方法可以实现吗?
  • 不是真的,你可以做一个,但是需要更多的资源和时间进行维护等,所以不推荐

标签: java php html apache


【解决方案1】:

在 .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

【讨论】:

  • 谢谢@Nick Vasic 我编辑了我的 .htaccess 并且仍然收到错误 404 。有什么我想念的吗?
【解决方案2】:

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

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

一个在您的脑海中:**您需要将您的“友好”网址嵌入到您发送给客户的任何 html 中。 ** .htaccess 不会对漂亮的 URLS 造成不良影响,它会使漂亮的 URLS 在内部重定向到真实站点。 这意味着您在网站上的链接需要包含:

<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]

如果某个地方的链接没有更改(外部资源),它会将用户重定向到漂亮的 URL。

【讨论】:

  • 这是我当前的 .htaccess 文件。它将“www.example.com/toronto.on”重定向到一个名为 cate.php 的页面,其中城市值 = 'toronto' 和省 ='on'。选项 +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com RewriteRule ^(.*)$ example.com/$1 [R=permanent,L]
猜你喜欢
  • 1970-01-01
  • 2011-03-27
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多