duandao3265 2018-10-09 09:30
浏览 37
已采纳

除了重写规则的一些网址

I inserted this rule in my .htaccess file:

RewriteRule ^([A-Za-z0-9-]+)/?$   index.php?content=category&categoryname=$1  [NC,L]

In this way I can get friendly urls like this: http://localhost/mysite/london

I'd also like to use a friendly url for my contact page like so: https://localhost/mysite/index.php?content=message to become: https://localhost/mysite/contact

But if I insert the below rule into .htaccess...

RewriteRule   ^contact/?$   index.php?content=message  [NC,L] 

...it doesn't work as it seems that the rule for the categories affects this rule.

In fact, if I comment out the category rule...

#RewriteRule   ^([A-Za-z0-9-]+)/?$   index.php?content=category&categoryname=$1  [NC,L]

...the url friendly rule for the contact page works (https://localhost/mysite/contact)

So I'm looking for the possibility to exclude some parameter from the category rule to allow for a redirect in some case to another url.

Thanks for any suggestions...

  • 写回答

1条回答 默认 最新

  • doupian9798 2018-10-09 09:47
    关注

    Firstly you need to make sure the contact rule is placed before the more generic one so htaccess can process it first:

    RewriteRule ^contact/?$ index.php?content=message [NC,L]
    RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
    

    If you have it the other way around the generic rule will always be looked at and match before htaccess even gets to the contact rule.

    Note though that the way you wrote your rules it will only match URLs such as

    https://exmaple.com/contact

    but not

    https://exmaple.com/contact/something-else

    However, looking at your more generic rule I assume this is intentional.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度