简体   繁体   English

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

[英]Using .htaccess to change directory show in url

I am trying to change the url that show in the addressbar. 我正在尝试更改地址栏中显示的网址。 Code in .htaccess: .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). 说明:同一目录中的所有页面和4个第一次重写规则都没问题,但最后一次重写规则有问题。(产品/ led-bulb目录不存在)。 Problem: last rule when loaded the [led-bulb.php] it not loaded any style and show the page without any style and design. 问题:加载[led-bulb.php]时的最后一条规则它没有加载任何样式并显示没有任何样式和设计的页面。

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: 您可以在页面HTML的<head>部分下面添加:

<base href="/" />

so that every relative URL is resolved from that base URL and not from the current page's URL. 以便从该基本URL解析每个相对URL ,而不是从当前页面的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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM