【问题标题】:Convert ugly urls to user friendly urls将丑陋的网址转换为用户友好的网址
【发布时间】:2013-05-20 12:24:39
【问题描述】:

在我的网站中有很多丑陋的 URL,现在我需要将它们重写为用户友好的好 URL。

这些是我网站上的一些丑陋的网址:

http://www.mydomain.com/profiles/tutors/index.php?code=1285&name=Ricky+Pointing
http://www.mydomain.com/profiles/centers/index.php?code=2285&name=City+&+Gills
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php.... and much more

所以我正在寻找对上述丑陋 URL 的用户友好的解决方案,如下所示:

http://www.mydomain.com/tutors/Ricky_Pointing.html
http://www.mydomain.com/centers/City_&_Gills.html
http://www.mydomain.com/about.html
http://www.mydomain.com/contact.html

我在我的 .htaccess 文件中尝试了类似的东西..

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/tutors/index.php\?code=([0-9]+)&name=([^&]+)&?([^\ ]+)
RewriteRule ^profiles/tutors/index\.php /%1/%2/?%3 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.+)/$ /profiles/tutors/index.php?code=$1&name=$2 [L,QSA]

但仍然没有运气。 任何人都可以帮我做到这一点吗? 谢谢。

【问题讨论】:

标签: .htaccess


【解决方案1】:

我认为你可能用RewriteConds 之类的东西过于复杂了。你可能会有这样的规则:

RewriteRule ^about\.html$ about.php [L]
RewriteRule ^contact\.html$ contact.php [L]
RewriteRule ^tutors/(.*)\.html$ /profiles/tutors/index.php?name=%1 [L]
RewriteRule ^centers/(.*)\.html$ /profiles/centers/index.php?name=%1 [L]

然后,您必须修改您的 PHP 脚本以使用该名称查找适当的内容,而无需依赖 code 的存在。它还必须处理原始 URL 中存在的下划线。

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 2012-09-24
    • 2012-10-15
    • 2011-02-15
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多