Forum Moderators: phranque

Message Too Old, No Replies

htaccess - ReDirects with URL’s with multiple ID

         

rixsweeney

1:38 pm on Sep 18, 2014 (gmt 0)

10+ Year Member



I can get a single id in a url to re-direct, but two ids in the URL is not working. Here is the htaccess code I am using: NOTE: The last line with two ids will not redirect for some reason...The Source url for this line is:
http://example.com/index.php?p1=about&p2=management

and it needs to re-direct to:
http://example.com/management.php


rewriteengine on
rewritecond %{HTTP_HOST} ^www.Example.com$ [OR]
rewritecond %{HTTP_HOST} ^Example.com$
rewriterule ^index\.html "http\:\/\/utilipath\.com\/index\.php" [R=301,L] #4a94724cc2bc0
Redirect 301 /about.php http://example.com/history.php
RewriteCond %{QUERY_STRING} ^p1=about?
RewriteRule ^index.php$ http://example.com/history.php? [R=301,L]
RewriteCond %{QUERY_STRING} ^p1=careers?
RewriteRule ^index.php$ http://example.com/careers.php? [R=301,L]
RewriteCond %{QUERY_STRING} ^p1=services?
RewriteRule ^index.php$ http://example.com/services-construction.php? [R=301,L]

RewriteCond %{QUERY_STRING} ^p1=about&p2=management?
RewriteRule ^index.php$ http://example.com/management.php? [R=301,L]

[edited by: Ocean10000 at 5:39 pm (utc) on Sep 18, 2014]
[edit reason] examplified [/edit]

phranque

8:49 pm on Sep 18, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, rixsweeney!


in general, your redirects should be ordered from most specific to most general.



rewriterule ^index\.html "http\:\/\/utilipath\.com\/index\.php" [R=301,L] #4a94724cc2bc0
Redirect 301 /about.php http://example.com/history.php

you don't want to mix mod_rewrite and mod_alias directives.
change the Redirect to an appropriate RewriteRule.

i wouldn't redirect anything to an index.php url.
instead you should redirect to the (trailing slash) directory url and then configure index.php as the default directory index document.

also your target url should not be quoted nor should it have backslash for escapes.

RewriteCond %{QUERY_STRING} ^p1=about?

the question mark in a regular expression means the previous character or capture group is optional.
i think it is not required in your application.

lucy24

6:49 am on Sep 19, 2014 (gmt 0)

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



the question mark in a regular expression means the previous character or capture group is optional.
i think it is not required in your application.

One place is is appropriate is in the first quoted group of conditions:
rewritecond %{HTTP_HOST} ^www.Example.com$ [OR]
rewritecond %{HTTP_HOST} ^Example.com$

can be reduced to
RewriteCond %{HTTP_HOST} ^(www\.)?Example\.com

but note that this is only necessary if there are subdomains you need to exclude. Otherwise just omit both the opening anchor and the (www\.)?

but two ids in the URL is not working

I do not understand what you mean by "ID" in this context. Are you referring to the RewriteCond citing a query string with two parameters?

rixsweeney

1:10 pm on Sep 19, 2014 (gmt 0)

10+ Year Member



What I am referring too is the ids (p1=about&p2=management) set up in the url for example:

http://example.com/index.php?p1=about&p2=management

I am trying to redirect this url with the following but its not working:

RewriteCond %{QUERY_STRING} ^p1=about&p2=management?
RewriteRule ^index.php$ http://example.com/management.php? [R=301,L]

The url with a single id I have working:
http://example.com/index.php?p1=about

Using the following:
RewriteCond %{QUERY_STRING} ^p1=about?
RewriteRule ^index.php$ http://example.com/about-us.php? [R=301,L]

Many thanks
Rick

lucy24

7:21 pm on Sep 19, 2014 (gmt 0)

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



I think sometimes when you say ? (optional element) in a pattern, you really mean $ (closing anchor). The bad news is that I strongly suspect some of your existing rules are working because of this mistake, not in spite of it.

When you have conditions looking at parameters, the safest format is

RewriteCond %{QUERY_STRING} (^|&)param1=val1($|&)
RewriteCond %{QUERY_STRING} (^|&)param2=val2($|&)
RewriteRule blahblah

In other words, look at each parameter-and-value pair separately, so it doesn't matter what order they are in or how many there are in total. The complicated-looking anchors
(^|&)
and
($|&)
* mean "this parameter-and-value set might be the very first thing in the query string, the very last thing in the query string, or separated by & from additional parameter-and-value sets".

In this sample
p1=about&p2=management

what's the "p1=about" for? It seems as if the redirect target is based purely on the "p2=management" part, so you don't even need to look at other parameters.


* ###. I have only just discovered that [ code ] markup can be used inline. This is a big help.