问题
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