开发者

using mod_rewrite to change url path

Is it possible to make a folder transparent in th开发者_开发问答e URL like in the following example: example.com/test/ changed to example.com/

using mod_rewrite?

I like having folders for organization, but I want a nice clean url.


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

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜