Rewrite Folder to another folder

New to mod_rewrite? This is a good place to start.

Rewrite Folder to another folder

Postby rdmips » Tue Sep 29, 2009 3:37 pm

Hi Guys,

I'm having some issue with those two rules :

RewriteRule ^/Website/(.+) /Website/$1 [NC,L]
RewriteRule ^/Website/?$ /Website/file.html [NC,R=301,L]

What I need to do is check the case insensivity and have an 301 redirect on the same server.

Example :
http://website.com/WeBsiTe ----> http://website.com/Website/file.html
http://website.com/WeBsiTe/ ----> http://website.com/Website/file.html

but when I try to type this on the browser and reload it :

http://website.com/WEbSite/file.html

it doesn't convert it in lowercase!!

Thanks a lot,
rdmips
 
Posts: 4
Joined: Mon Sep 28, 2009 4:03 pm

Postby richardk » Wed Sep 30, 2009 12:48 pm

Where are you putting the mod_rewrite? You probably need to remove the / from after the ^s.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule

RewriteRule ^website/?$ /Website/file.html [NC,R=301,L]

# If it starts with a w
RewriteCond $1 ^w [OR]
# or it has an uppercase letter after the W
RewriteCond $1 ^W.*[A-Z].*
# redirect to the correct case.
RewriteRule ^(website)/(.+)$ /Website/$2 [NC,R=301,L]

You should really just use all lowercase paths.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

folder to folder

Postby rdmips » Wed Sep 30, 2009 3:01 pm

Thanks a lot for your answer.

I added that code inside of my virtual host and not on the directory level. I cannot use .htaccess so I need to do it at virtualhost level.
The problem is the server will receive request with uppercase or lowercase and I cannot change those request so I need to check that part of the link.

Ex.

http://mywebsite.com/WeBsiTe ---> http://mywebsite.com/Website

Everything works but just when I try to type this :
Http://mywebsite.com/WeBsiTe/file.html doesn't work in the other cases works fine

this part "WeBsiTe" I need to be converted on "Website" and check the slash. After that add the file.html at the end of the link.

I will test it and let you know thanks a lot again.
rdmips
 
Posts: 4
Joined: Mon Sep 28, 2009 4:03 pm

Postby richardk » Wed Sep 30, 2009 4:59 pm

Then you will need those /s
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^/website/?$ /Website/file.html [NC,R=301,L]

RewriteCond $1 ^w [OR]
RewriteCond $1 ^W.*[A-Z].*
RewriteRule ^/(website)/(.+)$ /Website/$2 [NC,R=301,L]


Also, that
Code: Select all
RewriteRule

should not have been there.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rdmips » Wed Sep 30, 2009 6:11 pm

I have a lot of websites so I have to repeat everything twice. There is any dinamic way to do it for n websites?
I just need to check "WeBsIte" and convert in Website for a lot of sites.

Thanks
rdmips
 
Posts: 4
Joined: Mon Sep 28, 2009 4:03 pm

Postby rdmips » Thu Oct 01, 2009 5:20 am

Why are you using the rewritecond and checking just the first letter? I need to check all the string after http://website.com/"String"
rdmips
 
Posts: 4
Joined: Mon Sep 28, 2009 4:03 pm

Postby richardk » Thu Oct 01, 2009 8:46 am

I have a lot of websites so I have to repeat everything twice. There is any dinamic way to do it for n websites?
I just need to check "WeBsIte" and convert in Website for a lot of sites.

Do you have access to the httpd.conf file?
Do you have a server-side language available?

Why are you using the rewritecond and checking just the first letter? I need to check all the string after http://website.com/"String"

You only need to check "website" not the whole path (eg. /Webiste/Some/File/EtC.), right?

As long as it's only "website" there are two cases that require a redirect: one, when the first letter (w) is lowercase becuase it must be uppercase (the other characters don't matter because they will be fixed in the redirect). And two, when there is an uppercase character that is not the W. That is what the two conditions test for.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 30 guests

cron