Forum Moderators: phranque
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?
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
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]
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]
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
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]
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]
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]