使用mod_rewrite更改URL路径 [英] using mod_rewrite to change url path
问题描述
是否可以使URL中的文件夹透明,如以下示例所示: example.com/test/ 变成 example.com/
Is it possible to make a folder transparent in the URL like in the following example: example.com/test/ changed to example.com/
使用mod_rewrite吗?
using mod_rewrite?
我喜欢有用于组织的文件夹,但我想要一个干净的URL.
I like having folders for organization, but I want a nice clean url.
推荐答案
您可以使用mod_rewrite将此类内容放入根文件夹的.htaccess
文件中来实现:
You can achieve that with mod_rewrite putting something like this in the .htaccess
file of your root folder:
RewriteEngine on
# the first condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/test/.*
RewriteRule ^(.*)$ /test/$1
这将重定向到/test/
从/
请求的所有页面/文件.
This will redirect to /test/
all the pages/files requested from /
.
如果您只想重定向某些文件,请使用更具体的规则:
If you only want to redirect some files use more specific rule(s):
RewriteEngine on
# the condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/test/.*
# redirecting only images files and files that start with "doc"
RewriteRule ^(.*\.(jpg|png|gif))$ test/$1 [L]
RewriteRule ^(doc.*)$ test/$1 [L]
或者如果您想使用其他文件夹:
Or if you want to use different folders:
RewriteEngine on
# the condition is for avoiding an infinite loop
RewriteCond %{REQUEST_URI} !^/images/.*
RewriteCond %{REQUEST_URI} !^/docs/.*
# redirecting only images files and files that start with "doc"
RewriteRule ^(.*\.(jpg|png|gif))$ images/$1 [L]
RewriteRule ^(doc.*)$ docs/$1 [L]
这篇关于使用mod_rewrite更改URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!