0

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.

3
  • Changed how, using a redirect or an internal rewrite? Can you describe what is supposed to happen when a user enters the one URL, and the other? Commented Nov 13, 2010 at 0:02
  • Do you mean serving a file like root_folder/test/page.html as example.com/page.html? Commented Nov 13, 2010 at 0:13
  • @Pekka-internal rewrite would be best @Alberto-exactly Commented Nov 13, 2010 at 0:33

1 Answer 1

5

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.