Forum Moderators: phranque

Message Too Old, No Replies

Yet another htaccess redirect question

         

mralinush

8:59 am on Feb 9, 2012 (gmt 0)

10+ Year Member



Hi,

What rules I can use to redirect 301:

url.com/c/category-name/


to

url.com/category-name/


and

url.com/p/product-name.html


to

url.com/product-name.html


Thanks for helping!

g1smd

9:56 am on Feb 9, 2012 (gmt 0)

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



Use a RewriteRule for each.

Put the path part in the rule pattern, beginning with ^ and omitting the leading / of the path.

Put the new URL including protocol and hostname in the rule target.

Add the [R=301,L] flags.

If you have multiple URLs to redirect you can use RegEx patterns like ([^/]+)/$ and (([^/]+)/([^/]+))/$ and (([^/]+/)*[^/.]+)\.html to capture part of the requested URL and then put $1, $2 etc in the target URL to place those parts back into the new URL.

Be sure that the pages of your site also link to the new URL format. It is bad form to click a navigation link on a page of a site and then be redirected to a different URL within the same site.

There's thousnds of redirect examples in this forum. Let's see your code.

[edited by: g1smd at 10:02 am (utc) on Feb 9, 2012]

lucy24

9:58 am on Feb 9, 2012 (gmt 0)

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



You're not giving enough information.

I assume each of your two examples represents some group of page names in some consistent format. You'd need to work out exactly what the "keep" part looks like, and what the "throw away" part looks like. It's got to be something more complicated than changing

^[a-z]/(.*)

to

www.example.com/$1

or you wouldn't need to ask, you'd just skim through a few earlier threads.

mralinush

10:29 am on Feb 9, 2012 (gmt 0)

10+ Year Member



Basically I am trying to redirect all the old /c/ and /p/ from the URL and keep the rest.

I have been trying to use all the combinations I could with ^[a-z]/(.*) but I'm having problems with the fact that these are between / / , so I thought my approach is incorrect.

I'm not sure how to explain more than "redirect url.com/c/category to url.com/category" - for all possible names of "category".

g1smd

10:33 am on Feb 9, 2012 (gmt 0)

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



Let's see your previous "best effort".

There's likely a simple typo in a pattern, or a logic error creating a loop.

I guess you'll need (at least) two rules; one for folders and one for pages.

mralinush

10:57 am on Feb 9, 2012 (gmt 0)

10+ Year Member



Yes that's true there are 2 rules needed.

For categories, I tried something like

RewriteRule ^([^/]*/)?[^/]+([a-z])$ $1/c/$2 [QSA,L]


Without any results.

g1smd

11:05 am on Feb 9, 2012 (gmt 0)

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



You have a rewrite there, not a redirect. The patterns are also incorrect.

When user requests example.com, some optional folder levels (captured in $1), some non slash characters (not captured) followed by a single character (captured), fetch the content from the internal path $1/c/$2.

A request for
example.com/folder/somestuffherenow
will try looking in
folder/c/w
for content.

A request for
example.com//somestuff
will try looking in
/c/f
for content.

It looks like you have the rule exactly backwards.

You'll also find
([^/]+/)
for matching a folder level and
(([^/]+/)*)
useful for matching any depth of folder. The rule pattern will need to contain a literal /c/ in order to match it.

The redirect target will contain protocol and hostname and the rule will need the [R=301,L] flags to force a 301 redirect (default for R is 302)

mralinush

11:23 am on Feb 9, 2012 (gmt 0)

10+ Year Member



I know the purpose is to learn how to do this, but for me the only way I can learn this is by example and I've tried all the examples I could find and still couldn't get anywhere. That's how I got to that line I tried, which is probably absolute nonsense.

Can you show me an actual example of how this is done?

g1smd

11:48 am on Feb 9, 2012 (gmt 0)

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



You want
redirect
example.com/p/product-name.html
to
example.com/product-name.html


That will be
RewriteRule ^p/product-name\.html http://example.com/product-name.html [R=301,L]


For the more general version for any .html page you would need ([^/.]+) as the pattern and $1 as the substitute for the "product-name" part. The rest of the pattern and target are literal.

lucy24

9:35 pm on Feb 9, 2012 (gmt 0)

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



Are /c/ and /p/ the specific directories you're trying to get rid of? Just those two? Both top-level directories?

I have been trying to use all the combinations I could with ^[a-z]/(.*) but I'm having problems with the fact that these are between / / , so I thought my approach is incorrect.


But in the pattern, they're not between anything. The first slash-- the one after your domain name-- is omitted. So if your Rule is otherwise correctly written,
^[a-z]/(.*)

should be exactly what you want.

It will also get rid of directories /a/ /b/ /d/ and so on. If they don't exist, that is not a problem. If they do exist-- or if you just want to save a few nanoseconds of processor time-- you can replace [a-z]/ with a simple [cp]/

So let's see your current rule. Do you have any other rewrites or redirects in place? Do they work as intended? Did you remember to say

RewriteEngine on

? In rare cases, the server will insist on lower-case "o" on "on". Most of the time it makes no difference.