【问题标题】:convert dynamic url to seo friendly url in htaccess在 htaccess 中将动态 url 转换为 seo 友好的 url
【发布时间】:2017-07-13 16:56:07
【问题描述】:

当前网址是 https://www.website.com/colleges.php?id=NR%20School%20of%20Architecture

我们想将此 URL 更改为对 SEO 友好的 URL,如下所示, https://www.website.com/colleges/NR-School-of-Architecture

我们使用了这个 htaccess,但只删除了空格

RewriteEngine On
RewriteBase /
# replace all space by hyphen
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ %1-%2 [L,NE,R=302]

我们使用这个 htaccess 删除了 .php,它起作用了,并且整个站点中的 .php 被删除了。

但是我们会得到一个像这样的 seo 友好的 url, 学院/NR-建筑学院

RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

请帮助我们使用 htaccess 实现对 seo 友好的 url

提前致谢。

【问题讨论】:

  • 您是否做过任何研究并尝试过任何东西?一个关于“htaccess seo 友好的 url”的快速谷歌给了我多个点击和教程......
  • 我投票决定将此问题作为离题结束,因为它没有显示出任何试图自己解决问题的努力。
  • 是的,我们尝试了一些 htaccess 代码,但它不起作用
  • 那么您需要向我们展示您尝试了什么以及发生了什么。就像我在第一条评论中提到的那样,如果你只是谷歌的话,有很多关于这个的教程。
  • 上面的 htaccess 只删除了空间,但它使它成为一个 seo 友好的 url,并且数据库也可以与它连接,对于那个 url

标签: php .htaccess


【解决方案1】:

确认启用 mod-rewrite 后,您需要在 .htaccess 文件中定义重写规则,该文件位于您的站点文档根目录中

RewriteEngine On
RewriteBase /
RewriteRule ^home /index.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 2016-04-29
    • 2012-08-16
    • 2011-05-29
    • 2013-01-22
    • 2013-04-28
    • 2010-12-24
    • 2013-02-03
    相关资源
    最近更新 更多