I am wondering wether it's possible to use .htaccess
to rewrite a folder name.
What I mean is this.
Lets say I have a url like:
www.site.com/folder1/page.php
Now I want to rewrite the url to (for example)
www.site.com/apple/page.php
The folder1 is an existing folder on my webspace.
important: the "apple" is not a folder rather just a name!
Ok here is a step by step plan:
www.site.com/folder1/login.php
www.site.com/apple/login.php
This means that apple
is just a name and not a directory. All the code should just come from folder1. Acutally apple should just be an alias for folder1
. I can't just rename folder1
to Apple
. Therefor I would just rewrite folder1
to apple
.
htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.
Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.
mod_rewrite can only rewrite/redirect requested URIs. So you would need to request /apple/…
to get it rewritten to a corresponding /folder1/…
.
Try this:
RewriteEngine on
RewriteRule ^apple/(.*) folder1/$1
This rule will rewrite every request that starts with the URI path /apple/…
internally to /folder1/…
.
Edit As you are actually looking for the other way round:
RewriteCond %{THE_REQUEST} ^GET\ /folder1/
RewriteRule ^folder1/(.*) /apple/$1 [L,R=301]
This rule is designed to work together with the other rule above. Requests of /folder1/…
will be redirected externally to /apple/…
and requests of /apple/…
will then be rewritten internally back to /folder1/…
.
try:
RewriteRule ^/apple(.*)?$ /folder1$1 [NC]
Where the folder you want to appear in the url is in the first part of the statement - this is what it will match against and the second part 'rewrites' it to your existing folder. the [NC] flag means that it will ignore case differences eg Apple/ will still forward.
See here for a tutorial: http://www.sitepoint.com/article/guide-url-rewriting/
There is also a nice test utility for windows you can download from here: http://www.helicontech.com/download/rxtest.zip Just to note for the tester you need to leave out the domain name - so the test would be against /folder1/login.php
to redirect from /folder1 to /apple try this:
RewriteRule ^/folder1(.*)?$ /apple$1 [R]
to redirect and then rewrite just combine the above in the htaccess file:
RewriteRule ^/folder1(.*)?$ /apple$1 [R]
RewriteRule ^/apple(.*)?$ /folder1$1 [NC]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With