使用.htaccess更改URL中显示的目录 [英] Using .htaccess to change directory show in url

查看:103
本文介绍了使用.htaccess更改URL中显示的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改地址栏中显示的网址. .htaccess中的代码:

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]

说明:同一目录中的所有页面都可以,并且4个第一个重写规则都可以,但是最后一个重写规则有问题.(products/led-bulb目录不存在). 问题:加载[led-bulb.php]时的最后一条规则是,它不加载任何样式,并且显示没有任何样式和设计的页面.

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.

推荐答案

您的最后一条规则也运行良好,但是由于使用相对路径,您面临样式/图像显示问题.

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

您可以将其添加到页面HTML的<head>部分的下面:

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

<base href="/" />

,以便从该基本URL而不是从当前页面的URL解析每个相对URL .

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]

这篇关于使用.htaccess更改URL中显示的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆