【问题标题】:htaccess : rewrite urlshtaccess :重写网址
【发布时间】:2020-03-02 19:52:33
【问题描述】:

请帮忙改写网址

http://www.site.com/index.php?cat=catname

http://www.site.com/index.php?id=productname

变成这样

http://www.site.com/catnamehttp://www.site.com/productname

我认为它将需要 php 检查页面是否为 cat 将其作为 cat 使用 或其产品将其作为产品使用

【问题讨论】:

  • 纯粹关于服务器和托管配置问题的问题更适合 serverfault.com。我已投票决定将这个问题移到那里。您无需执行任何操作,但如果有足够多的其他用户同意我的观点,它将自动为您移动。

标签: .htaccess url-rewriting


【解决方案1】:

在 .htaccess 文件中:

RewriteEngine On

RewriteRule ^([0-9a-z\-\_]+)?$ /index.php?id=$1 [L,QSA,NC]

必须在 index.php 中进行类别/产品检查...

或者你可以添加额外的 slug

RewriteRule ^(category)/([0-9a-z\-\_]+)?$ /index.php?category=$1 [L,QSA,NC]
RewriteRule ^(product)/([0-9a-z\-\_]+)?$ /index.php?product=$1 [L,QSA,NC]

但网址看起来像http://site.com/category/catname

【讨论】:

  • 谢谢你的回答脏话,好的,我可以在 2 页中使用它吗index.php?cat= catname & product.php?name=productname 必须通过 htaccess 使用它才能像`site.com/catname`和`site.com/productname`这样可以工作 ==> RewriteRule ^([0-9a-z\-\_]+)?$ /index.php?cat=$1 [L,QSA,NC] RewriteRule ^([0-9a-z\-\_]+)?$ /product.php?name=$1 [L,QSA,NC]
【解决方案2】:

http://www.site.com/catnamehttp://www.site.com/productname

该方案的问题在于,您无法通过 URL 判断它是目录名称还是产品名称。因此,您可能会想要这样的东西:

http://www.site.com/Catalog/catnamehttp://www.site.com/Product/productname

然后可以使用以下规则在 .htaccess 文件中实现:

RewriteEngine On
RewriteRule ^Catalog/(.+)$ /index.php?cat=$1 [L]
RewriteRule ^Product/(.+)$ /index.php?id=$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2016-10-29
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多