Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite folders

mod_rewrite redirect lowercase dir to upercase dir

         

advancedgraphix

9:56 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



Hi, I've found some other posts similar to this problem, but I haven't managed to duplicate the success for my problem.
I'm trying to do the following:
Original URL:
domain.com/City/
and point any lowercased cities domain.com/city/ to the uppercase version of the URL.
I tried:
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^a/(.*) A/$1 [R=301,L]
RewriteRule ^b/(.*) B/$1 [R=301,L]
RewriteRule ^c/(.*) C/$1 [R=301,L]
For all letters, I want to be able to have both uppercase and lowercase pointing to that city, thanks.

I've also found this code:

RewriteEngine on
RewriteMap upper2lower int:tolower
RewriteRule ^/(.*)$ /${upper2lower:$1}

Would this work? Doesn't for me.

jdMorgan

10:49 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The second code snippet is intended for use in httpd.conf or conf.d -- the server configuration files. The RewriteMap directive is not available in .htaccess files.

The problem with you first code snippet is that "City/" does not start with any of "A/" "B/" "C/" or <anything>/ -- The pattern is simply incorrect. Fixing that and other syntax problems yields:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^a(.+)$ http://www.example.com/A$1 [R=301,L]
RewriteRule ^b(.+)$ http://www.example.com/B$1 [R=301,L]
RewriteRule ^c(.+)$ http://www.example.com/C$1 [R=301,L]

To restrict uppercasing to only those URL-paths specifying at least one subdirectory of more than one character, you might use:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^a([^/]+/.*)$ http://www.example.com/A$1 [R=301,L]
RewriteRule ^b([^/]+/.*)$ http://www.example.com/B$1 [R=301,L]
RewriteRule ^c([^/]+/.*)$ http://www.example.com/C$1 [R=301,L]

Jim

advancedgraphix

11:13 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



Hey Jim, I've tried both of those, which cause problems such as the css folder path gets transformed to Css so, that kills my path.

I only want my cities directories to be modified, to be able to point domain.com/phoenix to domain.com/Phoenix when someone types in he lowercase version, or some reason gets linked with the lowercase version...

Would it be better to write a line for each city individually? I do have all my cities in the state folder state/city but have written the url to show domain.com/city.

g1smd

11:29 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you want a redirect to the correct URL, or a rewrite? I would suggest a redirect is better. I would also go with all lower-case URLs.

If you are using a rewrite, you will eventually get multiple URLs indexed for the same content. That Duplicate Content will cause you problems.
So far you have used a redirect in your code. That is good.

If there are a small number of URLs that do not need to be modified, add a RewriteCondition that excludes them. Not is done with a ! in front.

advancedgraphix

11:45 pm on Sep 20, 2007 (gmt 0)

10+ Year Member



Ok, so a redirect would do then, I just want to be able to use my domain.com/City and have the lowercase ones redirect to it. That wouldn't cause google to index duplicate pages, I don't see why it would.

jdMorgan

12:28 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way or the other, you have to tell mod_rewrite how to identify what is to be rewritten/redirected, and what is not. So, the individual rules per-city is one way to go -- if inefficient, and another is to stick them all in some easily-detectable location or 'tag' the URLs in some way, such as /city/Phoenix or /Phoenix-city.

The key is that you must provide some way to differentiate between URLs that need to be redirected, and those that don't; mod_rewrite by itself doesn't know a city from a Citroen.

Jim

advancedgraphix

1:00 am on Sep 21, 2007 (gmt 0)

10+ Year Member



I do have all the cities in a folder, the state folder. Here is my current setup for that:

RewriteRule ^([^/]+)/$ /State/$1/ [L]

Thus giving me a url domain.com/city/ which I'm happy with. Just need to be able to let the server know to redirect the lowercase cities to the uppercase version.

jdMorgan

1:14 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The State/City tag must be in the URL -- the filepath is irrelevant to detecting whether or not to rewrite/redirect.

Jim

jdMorgan

1:24 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another option: Redirect only if the requested URL-path does not resolve to an existing file:

Options +FollowSymLinks
RewriteEngine on
#
# If file exists as requested, skip uppercasing (next 28 rules)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^[a-z].+$ [S=28]
#
# Else uppercase first letter of URL-path
RewriteRule ^a([^/]+/.*)$ http://www.example.com/A$1 [R=301,L]
RewriteRule ^b([^/]+/.*)$ http://www.example.com/B$1 [R=301,L]
RewriteRule ^c([^/]+/.*)$ http://www.example.com/C$1 [R=301,L]

Warning: This solution is inherently inefficient; Be sure to make the pattern in the file-exists-checking rule as specific as possible, in order to avoid doing the file-exists check except when necessary.

Jim

advancedgraphix

1:27 am on Sep 21, 2007 (gmt 0)

10+ Year Member



Here's an example:

http://www.example.com/florida

redirects you to:

http://www.example.com/Florida/

They have that with every state. Whats the best way I can get to that, still keeping my current setup of domain.com/city with the pages in state/city. Can't be done?

[edited by: jdMorgan at 3:01 am (utc) on Sep. 21, 2007]
[edit reason] example.com [/edit]

advancedgraphix

1:57 am on Sep 21, 2007 (gmt 0)

10+ Year Member



Well your example works :) But its changing all my folders to a capital letter. What now, how do I exclude folders from this condition?

I have about 17 root folders, is there an exclude condition?

advancedgraphix

2:10 am on Sep 21, 2007 (gmt 0)

10+ Year Member



Looks like:

Options +FollowSymLinks
RewriteEngine on
#
# If file exists as requested, skip uppercasing (next 28 rules)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/State/[a-z].+$ [S=28]
#
# Else uppercase first letter of URL-path
RewriteRule ^State/p([^/]+/.*)$ http://www.example.com/P$1 [R=301,L]

Works, is there a problem doing it this way?
domain.com/city to example.com/City even dough my real structure is example.com/state/city

[edited by: jdMorgan at 3:02 am (utc) on Sep. 21, 2007]
[edit reason] example.com [/edit]

jdMorgan

3:00 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Options +FollowSymLinks
RewriteEngine on
#
# If file or directory exists as requested, skip uppercasing (next 28 rules)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [b]^Sta[/b]te/[a-z].+$ [S=28]
#
# Else uppercase first letter of URL-path
RewriteRule ^State/a([^/]+/.*)$ http://www.example.com/A$1 [R=301,L]
...
RewriteRule ^State/z([^/]+/.*)$ http://www.example.com/Z$1 [R=301,L]

Jim

[edited by: jdMorgan at 3:00 am (utc) on Sep. 21, 2007]

g1smd

12:18 pm on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can have any URL structure you like, and it does not have to match the physical file structure on the server. That's what the rewrite does. It translates URLs into filepaths. You need a "marker" to simplify the process.

How about http://www.domain.com/city/Las.Vegas and http://www.domain.com/state/Florida type URLs. The "marker" makes it easy to process.

Make sure that you avoid spaces and underscores in multi-word names. Use a hyphen or dot between words.