Forum Moderators: phranque

Message Too Old, No Replies

rewriting url without directory

         

brianr

10:17 pm on May 22, 2009 (gmt 0)

10+ Year Member



Hi. We are launching a new site and the urls are a little bit different in that, on the new site, there is an extra "/categories" directory in the url so that a url on the old site like http://www.example.com/dir1 is now http://www.example.com/categories/dir1 and I'd like to rewrite the url so that the url looks like the old one. I'm not sure if this is the correct way to proceed with this but right now I have:


RewriteCond %{REQUEST_URI} ^categories/
RewriteRule ^/categories(.*) http://www.example.com/$1 [L]

I have tried this and other examples from other threads but can't get it working. www.example.com is actually an ip address since it's in the dev environment but hopefully that doesn't affect anything.

Also, on the old site the directory names are all lower case like /word1-word2 and on the new site they are /categories/Word1-Word2. It won't look good on the new site to have lower case in the html menus which is what this cms does so it looks like I will have to rewrite...if it's possible.

Any help is much appreciated.

g1smd

11:43 pm on May 22, 2009 (gmt 0)

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



By including a domain name and omitting R=301 you have actually got yourself a 302 redirect here.

Do you want a 301 redirect or a rewrite? They are both very different things, even though their code is similar.

Is there a change of domain name in this move from 'old site' to new site'? That's what I would imply by the use of those words. If so, then I assume this is code going on the old site, and hence I would guess you firstly need a 301 redirect from the old URLs to the new URLs. If there is only one domain for both sites involved here, then ignore this paragraph and do only the next listed steps:

I am guessing that you will need a rewrite to connect the new URL requests to the real internal filepath location of the new content.

Finally, I guess you need a redirect to stop the new content being directly indexed at it's real new folder location, redirecting people to the new URLs that you want to use.

Check recent threads. I've posted answers to at least three similar questions in the last three days...

jdMorgan

2:38 pm on May 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code is backwards, in that a request for /categories/xyz will be rewritten to an actual path of /xyz.

But there's a bigger problem here even when that is corrected, in that *all* URLs will be rewritten to the /categories subdirectory, making the root directory inaccessible via HTTP. So all files will have to be stored in /categories or its subdirectories.

If this is a problem, then you'll have to do more work to figure out whether the rewrite should be invoked. The most general way to do it is to check for "file or directory exists," either in root or in the subdirectory, depending on which one you want to override the other in the case where a resource is present in both:


# Rewrite all requests to /categories unless resource exists in root or rewrite was already done
RewriteCond $1 !^catagories/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /categories/$1 [L]
-or-

# Rewrite all requests to /categories if resource exists in /categories or rewrite was already done
RewriteCond $1 !^catagories/
RewriteCond %{DOCUMENT_ROOT}/categories/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/categories/$1 -d
RewriteRule ^/(.*)$ /categories/$1 [L]

Note that this code was written for use in httpd.conf or other server config files (as was yours). For use in .htaccess, remove the leading slash from the RewriteRule patterns.

Using mixed-case URLs is such a huge mistake that I can hardly comment further on it. However, you will likely need to use a script to do database lookups to get the "case translation."

Actually, you should fix the CMS to use separate database entries for link text (the links' "names" in the menu) and the associated URLs. Then you're free to use mixed-case link text with all-lowercase URLs. Fixing this now will avoid many problems in the future, not the least of which is that URLS have a restricted character set, which will limit what characters you can use in your link text if you use the same text for both.

Jim

brianr

5:15 pm on May 25, 2009 (gmt 0)

10+ Year Member



Thanks a lot for your replies.

Based on what you guys said I don't think I will be able to do it this way since there are other subdirectories (seem to be virtual subdirectories since I don't see them in the ftp and it looks like they go to through the index.php file the way this CMS is coded) on the new site and other links on the old site that don't follow any pattern will erroneously get rewritten. Also, there is the issue of different uppercase/lowercase letters. I guess I will have to put a separate redirect for each file on the old site as much as possible. Thanks again.