Welcome to WebmasterWorld Guest from 52.91.39.106

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

make 301 rule out of a rewrite

is it possible?

     
6:53 pm on Jul 7, 2016 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2240
votes: 8


Is it possible to create 301 redirect out of a rewrite rule?

I have some PHP based and database driven solution which has an option of SEO friendly URLs, but it does not redirect so both URLs exist. Canonical tags are in place, but I would want 301 as well.

So, if this is presented in the .htaccess:

RewriteRule content/([0-9]+)/([0-9]+)/([a-z\-]+)/(.+)\.htm(l?)$ index.php?action=artikel&cat=$1&id=$2&artlang=$3 [L,QSA]

Would it be possible to create a 301 rule based on the above?

Thanks
10:23 pm on July 7, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


Hi. Yes, this is possible. You just need to include the R=301 flag and make the substitution path root-relative - starting with a slash (or absolute with scheme and hostname - which some would say is preferable/safer). Making the substitution root-relative (or absolute) prevents the directory-prefix from being added back to the substitution, which would otherwise mess up the redirect.

So, for example:


RewriteRule content/([0-9]+)/([0-9]+)/([a-z\-]+)/(.+)\.htm(l?)$ /index.php?action=artikel&cat=$1&id=$2&artlang=$3 [R=301,L,QSA]


HOWEVER, I would question whether you really want to do this. You would never normally (externally) redirect from the "user-friendly" URL to the "ugly" (real) URL. If you are already linking to the "user-friendly" URL then this will create more work for your server and expose the "ugly" URL to your users.

If you did want to implement a redirect, it should be from the "ugly" URL to the "user-friendly" URL. Presumably the canonical tag already references the "user-friendly" URL. So, you could implement a redirect something like:


RewriteCond %{THE_REQUEST} \?action=artikel&cat=([0-9]+)&id=([0-9]+)&artlang=([a-z-]+)
RewriteRule ^index\.php$ /content/$1/$2/$3/SOMETHING.html [R=302,L]


This redirect should go before the internal rewrite.

Although, I'm a bit stuck what "SOMETHING" should be? You don't have a 4th backreference in your original substitution, so it's not possible to "reverse" without some more information?

This checks against THE_REQUEST server variable (the original request header sent to the server) in order to avoid a potential redirect/rewrite loop.

Change the 302 to 301 when you are sure it's working OK (302's aren't cached so makes testing easier/safer).
4:28 am on July 8, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15640
votes: 796


What happens to the last bit of the original request? As written (with, ahem, tiny corrections)
RewriteRule ^content/(\d+)/(\d+)/([a-z\-]+)/([^/.]+)\.html?$ /index.php?action=artikel&cat=$1&id=$2&artlang=$3 [L,QSA]
the last part of the request is simply thrown away, meaning that ANY kind of garbage would lead to valid content. Ta-daa, infinite URL space. Or was this a posting typo and there's really something like
&title=$4
at the end?

The QSA frankly worries me, because it implies that the original URL--the one that's supposed to be short and pretty--might also contain parameters.

Incidentally, if artlang = $3 then the third bit of the capture is probably really
[a-z][a-z](?:-[a-z][a-z])?
though it doesn't really matter so long as no other content could ever come in this position.

<irrelevant aside>
302's aren't cached

Funny you should say that. I've only just figured out why I get requests for pageA in an environment where they could only be asking for pageB (among other things, pageA is given as referer and I absolutely don't have those). It's because the user's browser is helpfully remembering that their previous request for pageB, five seconds earlier, was redirected to pageA. Oops. So I guess I could change the code to 302 ... except that the request is so infernally stupid, it's not worth it.
</ia>
12:16 pm on July 9, 2016 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2240
votes: 8


Thank you whitespace.

You would never normally (externally) redirect from the "user-friendly" URL to the "ugly" (real) URL.

The code from .htaccess I posted above is as is, used for rewriting the "ugly" URL.
When I write my own redirect code (301), it's always the "old" URL, followed by the desired one.
Now, I did come across rewrite rules where the desired URL is first, followed by the one we want to escape from, but never understood the difference between my 301 rules and such.

Although, I'm a bit stuck what "SOMETHING" should be? You don't have a 4th backreference in your original substitution, so it's not possible to "reverse" without some more information?

Sorry, I know there's a lot of database driven stuff, and I must admit I don't fully understand this.

Thank you lucy24.
The QSA frankly worries me, because it implies that the original URL--the one that's supposed to be short and pretty--might also contain parameters.

It does. Highlighting, search, etc.

Well, I can't connect the dots here. There seem to be too much of going on in the database and with the rewrite, so I'll wait for the solution owner to provide an update when it comes due.

Thanks again to all.
5:03 pm on July 9, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15640
votes: 796


When I write my own redirect code (301), it's always the "old" URL, followed by the desired one.
Now, I did come across rewrite rules where the desired URL is first, followed by the one we want to escape from, but never understood the difference between my 301 rules and such.

If I understood that right, they've got the target and pattern mixed up, so the rule is simply wrong and you can ignore it ;)

What you will typically have is two parts, a redirect and a rewrite. I've got a piece of boilerplate about this (search for the word "twostep"), but conceptually it's like this. Note that the redirect has a condition, while the rewrite doesn't:

RewriteCond %{THE_REQUEST} long-ugly-URL
RewriteRule long-ugly-URL http://example.com/short-pretty-URL [R=301,L]

RewriteRule short-pretty-URL /long-ugly-URL [L]
In your htaccess, make sure all the redirects ([R=301,L] flag) come first, with all the rewrites ([L] flag alone) later.

If "long-ugly-URL" has never existed as a publicly visible URL, you won't need the redirect, because nobody ever requests it in the first place. But check periodically to make sure it hasn't been accidentally exposed.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members