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.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法