Forum Moderators: phranque

Message Too Old, No Replies

Is it possible to redirect /1/ to /1/2.php

         

alexod

11:45 pm on Dec 21, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello

I'm on WP website. Is it possible to redirect domain.com/path/ to domain.com/path/page/

i try such code
RedirectMatch 301 /path/ /path/page/

it doesn't work

any advice will be appreciated !

phranque

2:17 am on Dec 22, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it doesn't work

assuming you request http://example.com/path/ what response do you get?

you don't really need a RedirectMatch for this, you can use a simple Redirect directive.
also note this...
http://httpd.apache.org/docs/2.2/mod/mod_alias.html#Redirect:
The new URL should be an absolute URL beginning with a scheme and hostname. In Apache HTTP Server 2.2.6 and later, a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.

so more like:
Redirect 301 /path/ http://example.com/path/page/

(depending on which version of apache)

however if you are using mod_rewrite directives anywhere in your configuration, you should probably use a RewriteRule directive instead.

lucy24

5:03 am on Dec 22, 2016 (gmt 0)

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



if you are using mod_rewrite directives anywhere in your configuration

If it's a WordPress site, there is no "if" about it. The reason your mod_alias rules (Redirect by that name) don't work is that by the time the server reaches mod_alias*, mod_rewrite has already executed, and everything has been handed off to WP.

Incidentally ... whenever I see a subject line such as
Is it possible to redirect /1/ to /1/2.php
my immediate reaction is: Yes, but why would you want to? But maybe that's just what your fingers typed, and you're not really planning to redirect /short-url/ to /longer-url/with-extension.


* For practical purposes, think "inverse alphabetical order".

alexod

4:42 pm on Dec 22, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



my immediate reaction is: Yes, but why would you want to? But maybe that's just what your fingers typed, and you're not really planning to redirect /short-url/ to /longer-url/with-extension.


The real purpose of this redirect is SEO -(silo-keywords). I have location page which have Short-URL, but i need to include keyword in URL and ... that's it.

So i need test how new URL works instead of domain.com/location/ try domain.com/location/keyword/ - that's the only reason.

>>>assuming you request http://example.com/path/ what response do you get?
####
Redirect 301 /location/ http://example.com/location/keyword/

I get url like this http://example.com/location/keyword/keyword/keyword/keyword/keyword/keyword/keyword/....

example.com redirected you too many times.
####

RedirectMatch 301 /location/ http://example.com/location/keyword/

OutPut - http://example.com/location/keyword/ - Empty page -
Domain.com page isn’t working - redirected you too many times.

lucy24

6:49 pm on Dec 22, 2016 (gmt 0)

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



RedirectMatch 301 /location/ http://example.com/location/keyword/
..
example.com redirected you too many times.

That's explainable-- and the explanation applies to all redirect methods, so it's worth remembering.

By your rule as written, all requests beginning in "/location/" are redirected to a new URL that still begins in "/location/". So when the new request comes in, it will get redirected all over again ... and again, and again, until the user's browser puts its foot down. (Anywhere from 10-30 times, depending on browser. I tested it once, but don't remember where I recorded the results.) If the code involved an internal rewrite instead of an external redirect, it would be the server that put its foot down after 30 or so rewrites, winding up with a 500-class error. (The user wouldn't be told the reason for the error, though your error logs will say.)

What you need to do is add a RewriteCond that excludes requests which have already been redirected. In fact you may even be able to do it without a Condition, if the URLs are as simple as they look here. For example:
RewriteRule ^location/?$ http://example.com/location/keyword/ [R=301,L]
See the $ sign? That's a closing anchor, meaning "this rule only applies if there's nothing else in the requested URL". The question mark is because search engines will ask for URLs without final slash, so you may as well redirect those at the same time.

Note that in mod_rewrite, unlike mod_alias, the pattern does not begin with a directory slash. Put this rule immediately before the WP section of your htaccess.

alexod

7:13 pm on Dec 22, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Lusy,

I love you ! :-)

I like how you explained the last part: about $ sign, question mark and directory slash in mod_rewrite.

thank you !