dpwqicw157673 2017-03-28 17:54
浏览 112
已采纳

使用.htaccess更改url中的目录show

I am trying to change the url that show in the addressbar. Code in .htaccess:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^Home?$  index.php [NC,L]
RewriteRule    ^about-us?$  aboutus.php [NC,L] 
RewriteRule    ^contact?$  contact.php [NC,L]
RewriteRule    ^products?$  products.php [NC,L]

RewriteRule    ^products/led-bulb?$  led-bulb.php [NC,L]

Explain: All page in the same directory and 4 first rewrite rule is ok, but the last rewrite rule has problem.(products/led-bulb directory not exist). Problem: last rule when loaded the [led-bulb.php] it not loaded any style and show the page without any style and design.

  • 写回答

1条回答 默认 最新

  • dongou6632 2017-03-28 18:01
    关注

    Your last rule is also working fine but you are facing style/image display problem due to your use of relative paths.

    You can add this just below <head> section of your page's HTML:

    <base href="/" />
    

    so that every relative URL is resolved from that base URL and not from the current page's URL.

    Also your rules should be making trailing slash optional like this:

    RewriteEngine On
    
    RewriteRule ^Home/?$ index.php [NC,L]
    RewriteRule ^about-us/?$ aboutus.php [NC,L] 
    RewriteRule ^contact/?$ contact.php [NC,L]
    RewriteRule ^products/?$ products.php [NC,L]
    
    RewriteRule ^products/led-bulb/?$  led-bulb.php [NC,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失