Using .htaccess to change directory show in url

这一生的挚爱 提交于 2019-12-24 09:28:25

问题


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:


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]


来源:https://stackoverflow.com/questions/43076620/using-htaccess-to-change-directory-show-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!