homepage Welcome to WebmasterWorld Guest from 50.16.112.199
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

    
A problem with mod_rewrite
jeddi



 
Msg#: 4571936 posted 5:28 pm on May 8, 2013 (gmt 0)

Hi,

I have been trying several variations to get
my server to redirect - but all seem to fail. :(

This is my url:

"huge-big-widgets.example.com/big-widgets/p1c9.html"

Now the data I want to collect in variables is the portion after "huge-big-widgets.example.com/" so in this case
I want "big-widgets" and "p1c9"

Then using the collected variables the server can redirect to:
"example.com/index.php?p=1&c=9&k=big-widgets"


I have thried this in my .htaccess file:

Options +SymLinksifOwnerMatch
RewriteEngine On

RewriteRule ^[\.0-9-a-z]+/([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]


I also tried :

RewriteCond %{HTTP_HOST} ^[.+].example.com/([-a-z]+)/[.+]$ [NC]
RewriteRule ^[\.0-9,:\/-a-z]+p([0-9]+)c([0-9]+)\.html$ index.php?p=$1&c=$2&k=%1 [NC,QSA,L]


As far as I can see, both should work,
but neither do.

This is the webpage:

[url]http://example.com/index.php?p=1&c=9&k=big-widgets[/url]

If you click on the Category "Big Widgets"

You will see this url:

"http://huge-big-widgets.example.com/big-widgets/p1c9.html"

The htaccess does not properly redirect.

Can anyone see my mistake ?


Thanks .


.

[edited by: phranque at 1:01 pm (utc) on May 11, 2013]
[edit reason] exemplified hostnames and keywords [/edit]

 

Dideved



 
Msg#: 4571936 posted 6:20 pm on May 8, 2013 (gmt 0)

First, a quick breakdown on why your current rewrite rule doesn't work.

^[\.0-9-a-z]+/([-a-z]+)/

To start off, you're matching two path segments, but in your example URL, there's just one: "monster-bikes". Perhaps you thought you were also matching the domain name? If so, that's not how rewrite rules work. You match on only the path.

p([0-9]+)pg([0-9]+)\.html$

The letters in your example URL are "p" and "c", but here you're matching on "p" and "pg".

This should work:

RewriteRule ^([^/]+)/p(\d+)c(\d+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]

[^/]+ means one or more non-slash characters. In other words, this matches a path segment. And \d is a shortcut for [0-9].
jeddi



 
Msg#: 4571936 posted 7:09 pm on May 8, 2013 (gmt 0)

Thanks.

You are correct, I had thought was dealing with the
full url, not just the path.

Thanks again. :)

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4571936 posted 8:07 pm on May 8, 2013 (gmt 0)

Overlapping previous posts, so this is purely a reply to your first:
Now the data I want to collect in variables is the portion after "www.example.com/" so in this case
I want "monster-bikes" and "p1c9"

Then using the collected variables the server can redirect to:
"example.com/index.php?p=1&c=9&k=monster-bikes"

{snip}

RewriteRule ^[\.0-9-a-z]+/([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]

Several mistakes here.

Preliminary mistake: Your post says "redirect" but the rule creates a rewrite. That means the user's address bar won't change, but you secretly fetch content from where it lives. As it happens, this is good. You don't want a redirect; you want a rewrite (short pretty URL, long ugly "real" location). Just make sure you've got the terminology straight.

In the RewriteRule itself, the first mistake is the one that prevents the rule from ever matching: the initial
[\.0-9-a-z]+/
From your example it looks as if this represents the hostname. This isn't used in the body of a RewriteRule-- in fact it's hardly ever used at all in per-directory contexts such as htaccess. Leave it out.

Incidentally, literal periods don't need to be escaped inside grouping brackets. (This is a general Regular Expressions rule, not specific to mod_rewrite.) It won't do any harm; it just isn't necessary.

This part alone
([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$
should work. Use an opening anchor with no leading slash. Most Apache installations are happy to let you use \d instead of [0-9] at a savings of three bytes. But if you're not very experienced with Regular Expressions, the byte savings may be outweighed by your personal reaction of "What the ### does \d mean?"

The text of your post says the URL is "p1c2", but the quoted rules have "pg" in place of "c". I assume this is a simple typo.

The target of a rewrite should have a leading / slash. NOT the full domain name! Just the slash.

What's the "QSA" for? Is there any possibility that the original request already contained a query string? If not, leave off the flag. The server has to go looking for a query, and it will never find one. And in the present rule, it seems as if the whole point is that you start out with a nice query-less URL; the query gets added behind the scenes.

Also leave off the [NC] flag. This flag should never be used with rules that create a rewrite instead of a redirect. It doesn't prevent the rule from working; instead it allows the rule to work with an infinitude of different URLs. (Look up Duplicate Content.) It also means your php script has to do extra work, as it now has to regularize all those randomly cased parameters.

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