homepage Welcome to WebmasterWorld Guest from 54.226.191.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess redirect is not working with [R=301,L]
aspirevb




msg:4688702
 7:00 am on Jul 18, 2014 (gmt 0)

I have added the following redirect code to my .htaccess file

RewriteEngine on
RewriteBase
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^products/([A-Za-z0-9-]+)/?$ product.php?id=$1 [R=301,L]

I am facing an issue with redirection when specified [R=301,L] in the code. Redirection is working with [NC] instead of [R=301,L]

anybody can help me ?

 

phranque




msg:4688849
 11:54 am on Jul 18, 2014 (gmt 0)

welcome to WebmasterWorld, aspirevb!


what request is being made?
what response did you get?
what were you expecting?

lucy24




msg:4688936
 6:48 pm on Jul 18, 2014 (gmt 0)

Redirection is working with [NC] instead of [R=301,L]

Do you mean [NC] instead of [R=301,L] or in addition to [R=301,L]? If it's an added NC then you need to change the pattern to match the actual casing of the URL. If you've removed the [R=301,L] and the rule appears to work, where before it didn't, then there's actually some other, later rule successfully creating a redirect.

When a rule creates an external redirect, it's easy to test it by changing the target to something like
www.example.com/hardeharhar.html
and then verify that that's what your address bar says.

:: wait, stop, rewind ::

product.php?id=$1

Is that supposed to be your target? That won't work. In combination with the missing RewriteBase, it creates a redirect to a filepath that you'll probably find in your Error Logs. By the usuall yawn-provoking coincidence, I experimented on this just a few days ago.

The target of an external redirect should always be the full protocol-plus-domain. For one, it eliminates the risk of needing a second redirect later if the request used the wrong form; for another, it saves the server the trouble of checking what the requested host actually was.

penders




msg:4689121
 2:33 pm on Jul 19, 2014 (gmt 0)

It looks like you are rewriting from a "user-friendly" URL to the actual/underlying URL. In which case you wouldn't normally want to "redirect" here? You would "internally rewrite" instead. Are you sure you want to externally redirect?

Redirection is working with [NC] instead of [R=301,L]


Unless you have other directives in your .htaccess file then this would result in an internal rewrite (not a redirect). Which URL do you see in the browsers address bar? And yes, this could well "work" in this instance (from the experiments that lucy24 mentions above from a few days ago). Just to reiterate... You are rewriting to a relative path and you have no RewriteBase (at least you are not specifying a value for the RewriteBase!).

lucy24




msg:4689164
 7:17 pm on Jul 19, 2014 (gmt 0)

you have no RewriteBase (at least you are not specifying a value for the RewriteBase!)

Further experimentation-- which I do realize I should have done in the first place-- suggests that the line
RewriteBase
(and nothing more) must be a typo in the post. In real life it leads to a 500 error for all requests.

aspirevb




msg:4689366
 6:59 am on Jul 21, 2014 (gmt 0)

Thanks for the reply lucy24. I have used [[R=301,L] independently, it is not combined with [NC]. I have modified RewriteBase to the root directory as suggested. Now i am redirecting to the 404 Error page. Please see the modified code below:

RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^products=1$
RewriteRule ^products/([A-Za-z0-9-]+)/?$ abc.php?products=$1 [R=301,L]

penders




msg:4689395
 8:22 am on Jul 21, 2014 (gmt 0)

I have modified RewriteBase to the root directory as suggested.


Was it not like this in your original script?

If you are getting a 404, then it would seem "http://example.com/abc.php" doesn't exist. What URL are you wanting to redirect to?

phranque




msg:4689401
 8:32 am on Jul 21, 2014 (gmt 0)

RewriteRule ^products/([A-Za-z0-9-]+)/?$ abc.php?products=$1 [R=301,L]

as lucy24 suggested previously, the target of an external redirect should always specify the canonical protocol and hostname.

aspirevb




msg:4689461
 11:15 am on Jul 21, 2014 (gmt 0)

Thanks @phranque - i have modified the code with canonical hostname as lucy24 suggested. please see the modified code below:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.examplesite.com
RewriteRule ^products/([A-Za-z0-9-]+)/?$ abc.php?products=$1 [R=301,L]

still i am facing the issue to redirect to 404 page. any suggestions would be welcome. thanks in advance.

penders




msg:4689466
 11:34 am on Jul 21, 2014 (gmt 0)

Thanks @phranque - i have modified the code with canonical hostname as lucy24 suggested.


Errrm, no you haven't. "the target of an external redirect", for example...

RewriteRule ^products/([A-Za-z0-9-]+)/?$ http://example.com/abc.php?products=$1 [R=301,L]
aspirevb




msg:4689473
 12:07 pm on Jul 21, 2014 (gmt 0)

Hey @phranque, i have modified the code as you suggested . Now the redirect rule is not working. when i type the seo friendly url in the browser it is modifying to the original url.
For ex: if i type http://example.com/products/123 it is modifying to original url http://example.com/abc.php?products=123

penders




msg:4689478
 12:48 pm on Jul 21, 2014 (gmt 0)

To repeat phranque's initial post...

what were you expecting?


If it's redirecting to "http://example.com/abc.php?products=123" then the code you have posted would appear to be "working".

However, as I mentioned in my initial post... if this is an "seo[sic] friendly url" then why are you redirecting? Or is that not the intention? (To test maybe...?)

aspirevb




msg:4689484
 1:16 pm on Jul 21, 2014 (gmt 0)

Hey @penders, Let me brief the details of the requirement. Few months back i have hosted my web site but i was not able to create seo friendly url's since i was busy with another work. what i did now is i created the seo friendly url's on the web site and added rewrite rules for it in .htaccess file. the issue is redirection is working fine with type [NC] to the rules and not for [R=301,L].

lucy24




msg:4689513
 3:38 pm on Jul 21, 2014 (gmt 0)

I think the part nobody can figure out is why you would even want to redirect

example.com/products/blahblah
to
example.com/product.php?id=blahblah

If you've got the rewrite in place, the job is done. For insurance you can add a redirect in the other direction, so when someone explicitly asks for
product.php?id=blahblah
(RewriteCond looking at THE_REQUEST)
they will be sent over to
products/blahblah
but that's a different rule.

not2easy




msg:4689517
 3:43 pm on Jul 21, 2014 (gmt 0)

By any chance is this a WordPress site?

penders




msg:4689519
 3:47 pm on Jul 21, 2014 (gmt 0)

For insurance you can add a redirect in the other direction...


Exactly. Which, as it happens, would seem to be necessary in this case because (I assume) the "unfriendly" URLs have been indexed (because the "friendly" URLs were only implemented later).

aspirevb




msg:4689661
 6:07 am on Jul 22, 2014 (gmt 0)

Yeah @penders, you are right. the unfriendly url's already indexed. what i thought was specifying the [R=301,L] in rewrite rule make the search engines understood that the earlier pages are moved to the new pages. Am i right in specifying [R=301,L] or it is not required?.

not2easy




msg:4689665
 6:44 am on Jul 22, 2014 (gmt 0)

If you do not specify [R=301,L] it will not be a 301 (Permanent), it will be a 302 (Temporary) change by default.

But if you do not correct the rules you have there, you are redirecting TO the old 'ugly' URLs.

lucy24




msg:4689667
 6:59 am on Jul 22, 2014 (gmt 0)

If you use neither [R] of some sort nor full protocol-plus-domain in the target, it will not be an external redirect at all; it will be an internal rewrite. You need one of each, going in opposite directions.

what i thought was specifying the [R=301,L] in rewrite rule make the search engines understood that the earlier pages are moved to the new pages.

This makes me very uneasy, because it sounds as if you've got things precisely backward.

PATTERN = stuff on the left = input
TARGET = stuff on the right = output

aspirevb




msg:4691238
 12:07 pm on Jul 28, 2014 (gmt 0)

Thanks lucy24. I have modified the Rewrite condition as mentioned. the redirection is working now.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved