3

I have a subfolder I want to redirect to, I want it to redirect dynamically so anytime I go to:

example.domain/about it takes me to example.domain/subfolder/about, but still shows: example.domain/aboutin the address bar

However, I don't want to have to specify a domain name so I can use it in any project. So I suppose just automatically want to declare the root domain name as a variable?


I have this working here:

RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L,R=301]

And it doesn't require the domain name, but it still shows subfolder in the domain name.


The strategy in this answer is what I need, but minus the domain name:

RewriteEngine On
RewriteRule ^/subfolder/(.*)$ http://www.example.domain/$1 [L,R=301]

I also need it to work for both the www. and the naked domain.

2
  • Try to remove R=301 which is for redirecting it to the destination rule Commented Jan 28, 2016 at 14:58
  • Thanks guys, that works for the index file e.g. when I go to /, but it doesn't seem to work for any /other page Commented Jan 28, 2016 at 15:02

1 Answer 1

2

Try the following rule :

RewriteEngine On
#remov php exten
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1.php -f
RewriteRule ^(.*)$ /subfolder%{REQUEST_URI}.php [NC,L]
#redirect root to sub
RewriteRule ^((?!subfolder).*) /subfolder%{REQUEST_URI} [L,NC]
Sign up to request clarification or add additional context in comments.

2 Comments

That works! Thank you :) Is there a way I can combine that with not requiring .php extension? I'm using RewriteCond %{REQUEST_FILENAME} !-f #RewriteRule ^([^\.]+)$ $1.php [NC,L]
Oh, actually is there anyway to override it for script, image and stylesheet paths that are in the root? Or is the only way to move these into the subfolder?

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.