Welcome to WebmasterWorld Guest from 52.91.39.106

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Why is Mod rewrite a variable not getting passed ?

Mod rewrite a variable not getting passed?

     
10:00 pm on Aug 28, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


Hi

I thought I had got this correct but it seems my variable
is not getting passed

My input url is:

example.com/discounts/2bEcQPm-a-Big-Blue-Widget.html

The result I want is :
http://example.com/2bEcQPm

and this is the -haaccess

# REDIRECTING TO SITE with SHORT CODE E.G http://example.com/2bEcQPm
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^discounts/(.*)-a- http://example.com/$1

But I get a 404 error !

I have tried putting escapes in front of the periods like this :

# REDIRECTING TO SITE with SHORT CODE E.G http://example.com/2bEcQPm
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^discounts/(.*)-a- http://example.com/$1

AND WITH NO escapes, like this:

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^discounts/(.*)-a- http://example.com/$1

BUT I STILL GET THE 404.

I checked that http://example.com/2bEcQPm does actually
go to the correct page, and it does.

So I do seem to have a problem with this simple rewrite

Any suggestions ?

Thanks

[edited by: not2easy at 10:13 pm (utc) on Aug 28, 2016]
[edit reason] please use example.com/avoid specifics [/edit]

11:02 pm on Aug 28, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

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


but it seems my variable is not getting passed


What do you mean exactly? What is the actual result you are seeing? Are you being redirected? I assume an external redirect is required (by the inclusion of the absolute URL), not an internal rewrite? (Although, a rewrite wouldn't really make sense in this case?)

example.com/discounts/2bEcQPm-a-Big-Blue-Widget.html


You state the input URL is "example.com", but you are checking for "www.example.com" in your condition.

and this is the -haaccess


I assume that's a typo? But you've not made reference to .htaccess anywhere else? It should be .htaccess

I have tried putting escapes in front of the periods like this :


Yes, you should backslash escape literal dots in regular expressions (regex).


RewriteRule ^discounts/(.*)-a- http://example.com/$1


I don't know what else you have in your .htaccess file, but you probably should have the L flag on the RewriteRule. And if this is intended to be a redirect, you should be explicit and use the R flag. (In the absence of the R flag, the docs appear to suggest that when specifying an absolute URL where the scheme and hostname matches that of the current request then these are stripped and the request is internally rewritten. However, in my experience, when an absolute URL is given, the requested is always externally redirected - so this is what I assume is happening here.)

Also, as lucy24 suggested in your earlier thread [webmasterworld.com], \w+ would be preferable to the all encompassing .*

So, for example:


RewriteRule ^discounts/(\w+)-a- http://example.com/$1 [R,L]


If this is intended to be a permanent redirect then you must explicitly write R=301 in place of R (which defaults to a 302).

I checked that http://example.com/2bEcQPm does actually go to the correct page, and it does.


So, you do have additional directives in .htaccess in order to route the request - there might be a conflict, depending on the order of these directives?
5:59 am on Aug 30, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


Hello Whitespace,
Thanks for your detailed reply.

Firstly, Yes this is an external redirect.

I think I have got it to work now ... just one thing ...

Considering that I am using a code with numbers and letters eg 2bEcQPm
Would the discounts/(\w+)-a work ? I thought the" \w" was for alphabetical words ?

I hope this post get displayed OK.

Thanks again.
6:55 pm on Aug 30, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

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


Considering that I am using a code with numbers and letters eg 2bEcQPm
Would the discounts/(\w+)-a work ? I thought the" \w" was for alphabetical words ?


The \w shorthand character class matches "word characters". This is "word" in a broad (computer) sense, as it does include numbers. Specifically, it is short for [A-Za-z0-9_] - so upper and lowercase letters, numbers and the underscore. And the + is for 1 or more. Whereas * is 0 or more (so .* could match nothing).

With PCRE (the regex flavour used by Apache) you'll need to be verbose if you want to match only letters. eg [A-Z] or [a-z] or [A-Za-z]. (In some flavours of regex there are shorthand character classes for these too eg. \u and \l.)
9:32 pm on Aug 30, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


thnaks very much :)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members