Forum Moderators: phranque

Message Too Old, No Replies

rewrite portion of URL to make it shorter

         

Skhan00

2:35 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



i tried looking at tutorials, but couldnt get this figured out.

lets say this is my url structure

www.mysite.com/path1/path2/indexpage (the site doesnt use extensions on the page names)

i want to replace "/path1/path2/" with "/site/" (or remove/hide it completely if possible)

so that its www.mysite.com/site/indexpage (or www.mysite.com/indexpage if possible).

I also want to 301 redirect all the urls with the old path to the new one so that i dont get duplicate content issues.

is this possible?

jdMorgan

4:47 pm on Jul 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's possible. But first you'll need to replace all of the old URLs on your pages with the new shorter ones. Then internally rewrite those shorter URLs, when requested by a client with a 'click' on your page's links, to the longer filepaths on your server, so that content can be delivered.

Finally, you can 301-redirect direct client requests only for the longer old URLs to the new shorter ones. This step is optional, and is only needed if you are in a hurry to change the URLs shown in search engine results listings, and to help prevent people from creating new links to the old URLs.

The key to detecting direct client requests only is to examine the server variable %{THE_REQUEST} using a RewriteCond; This allows you to differentiate an external client request for an old/long URL from an internal request for the unchanged long filepath as the result of the internal rewrite. Without this check the rewrite and the redirect would interact, leading to an infinite loop.

Several detailed examples of this process have been posted here. One example thread that describes this is about Changing Dynamic URLs to Static URLs [webmasterworld.com], but almost all of the same techniques and warnings apply.

For background information, see the threads posted in our Apache Forum Library, and the references cited in our Apache Forum Charter. Take a look through the thread above and these other resources, and then feel free to post specific questions back here.

Jim

Skhan00

3:18 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



quick question, i cant get this rewrite rule to work, am i doing this properly?

RewriteRule ^/dir1/dir2/prod-search?cid=9379&BRAND_NAME=InHouse\+Brands$ /dir1/dir2/new-search [R=301,L,NC]

i am trying to 301 redirect the following URL: "www.domain.com/dir1/dir2/prod-search?cid=9379&BRAND_NAME=InHouse+Brands" to "www.domain.com/dir1/dir2/new-search"

[edited by: Skhan00 at 3:19 pm (utc) on July 14, 2008]

g1smd

3:43 pm on Jul 14, 2008 (gmt 0)

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



If it is in .htaccess then remove the leading "/" from your rule.

You'll also need to separately check the %{QUERY_STRING} as that isn't a part of the URL.

As it is a redirect, then the target URL should contain the full domain name too (otherwise the redirect will not fix www and non-www issues).

[edited by: g1smd at 4:17 pm (utc) on July 14, 2008]

jdMorgan

4:07 pm on Jul 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Skhan00,

g1smd is referring to using a RewriteCond to examine %{QUERY_STRING} for specific script parameter values. Since query strings are not part of the URL, but rather, data attached to that URL to be passed to the resource at that URL, they are not 'visible' to RewriteRule. So you must use a separate RewriteCond line to examine the query string. See the mod_rewrite documentation for further info.

Jim

Skhan00

6:49 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



hey g1smd, this is actually going into a rewrite.conf file
and as for the www and non-www, this is already fixed using another rewrite rule

so i should do it this way?:

rewriteCond %{QUERY_STRING} ^cid=9379&BRAND_NAME=InHouse\+Brands$
rewriteRule ^/dir1/dir2/prod-search$ /dir1/dir2/new-search [R=301,L,NC]

g1smd

6:57 pm on Jul 14, 2008 (gmt 0)

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



OK, on the leading "/". That's fine. It's just that 90% of people are using .htaccess and having the "/" there is a "common error" that stops things working. You've got that one licked; I should have given a slightly longer answer.

Yes, you need to check the Query String in the RewriteCond, and the rest of the URL in the RewriteRule. Barring typos, that one looks OK to me.

.

*** and as for the www and non-www, this is already fixed using another rewrite rule ***

Yes, and that means for some URLs, you will generate a Redirection Chain... where there are two redirects back to back. That must be avoided.

There is a better way to do this. It is to simply have this "specific" redirect *first* and let it also fix up the non-www and www for *this* URL at the same time.

You will then have your more general non-www to www redirect *after* this redirect and will let it catch all of the *other* URLs that need fixing.

That is, don't let the first redirect do only half of the job, and then have to invoke the other redirect to do the other half. Do it all in the first one for your specific Query String URL.

Having a double redirect is a very bad idea. It can very easily be avoided.

.

General plan:

Redirects first: most specific through to more general.

Rewrites last: most specific through to more general.

Use RewriteRule for all of them; using Redirect for some would not be able to guarantee processing order.

.

Beware of mixed case URLs. It's a ticking Duplicate Content time bomb. If you can, go all lower case.

[edited by: g1smd at 7:05 pm (utc) on July 14, 2008]

jdMorgan

7:02 pm on Jul 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to clear (remove) the query string, then append a "?" to your substitution URL.

Otherwise, the query string will pass through the rewrite unchanged.

To prevent future server portability problems, stick with the documented casing: RewriteCond, RewriteRule

Jim

Skhan00

7:08 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



mm, understood, let me go research on how to do this, and if you have any good places please post.

learning this one step at a time and thanks for the advice, much appreciated.

this mod_rewrite is pretty complex in what it can do and to learn it

edit: i tested the above, small problem
it appends the query

Edit 2: oops thanks jdmorgan

[edited by: Skhan00 at 7:14 pm (utc) on July 14, 2008]

g1smd

7:37 pm on Jul 14, 2008 (gmt 0)

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



Yes, I forgot to add the question mark.

So, your query string redirect now has to fix three problems all at the same time.