using mod_rewrite to change url path

天大地大妈咪最大 提交于 2019-12-12 01:18:59

问题


Is it possible to make a folder transparent in the 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.


回答1:


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]


来源:https://stackoverflow.com/questions/4170073/using-mod-rewrite-to-change-url-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!