Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect problem

htaccess redirect problems

         

Brandie

6:31 pm on Jan 9, 2012 (gmt 0)

10+ Year Member



Please help a newbie who is rapidly losing the will trying to figure out a redirect instruction to go in my htaccess file.
My site has lots of dynamically-generated pages with SEO-friendly urls. On page request, I want to direct the request to two handling scripts, depending upon the URL called. These process and generate the page requested. These are not permanent 301 url changes or anything – purely an redirect to specific handling scripts.
Typical urls will be…
[mydomain.com...]
[mydomain.com...]

Page addresses without a number in the name are handled by /scripts/handler1.php?N= + the parameter ‘apples.htm’. Pages that contain a number are handled by handler2.php with the additional parameter of P= the number contained in the URL. So, these two demo addresses should be handed over to …

/scripts/handler1.php?N=apples
/scripts/hander2.php?N=pears&P=1

I haven’t got as far as trying to figure out how to split between the two scripts because I can’t even get the basic redirect to handler1.php to work. I’m testing on a Windows /Wampserver machine which possibly works a little differently to the live server, which in LAMP. However, I had assumed that the following would work – but it doesn’t…

RewriteRule ^fruit/([a-zA-Z0-9\_\.\&-°]*).([a-zA-Z0-9\_\.\&-°]*)$ /scripts/handler.php? N=$1 [L]

Any help to put me on the right road will be much appreciated.

g1smd

7:06 pm on Jan 9, 2012 (gmt 0)

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



The . in the pattern matches ANY single character, rather than only a literal period.

Escape it to ensure it matches a period.

The * allow the parts to each be blank. So example.com/fruit/. with a trailing period would be a valid URL with your patterns. Replace the * with a + here.

By allowing a period in each part and as a separator, the RegEx parser cannot hope to work out which period goes in $1, which is the separator and which goes in $2.

Additionally, by not checking the value of $2 you promote duplicate content. A request for
example.com/fruit/apple.html
will show the same content as
example.com/fruit/apple.junk
and
example.com/fruit/apple.spam-keywords-here
.

lucy24

12:41 am on Jan 10, 2012 (gmt 0)

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



[a-zA-Z0-9\_\.\&-°]

Say what now?
Lower-case letters: check.
Capital letters: check.
Numerals: check.
Orphaned backslash: doesn't belong.

Lowlines never need to be escaped; they have no special meaning either in RegEx or in mod_rewrite. The same goes for ampersands.

Literal periods inside grouping brackets don't need to be escaped.

&-° means all characters from ampersand through degree sign, inclusive. This cannot possibly be what you mean. The ampersand comes at the beginning of the ASCII block, so &-° means:
&'()*+,-./:;<=>?@{|}~¤¥¦§¨©ª«¬®¯°
... plus all the alphanumerics you've already listed, the "soft" hyphen and the nonbreaking space. If you've got all those in your URLs, you are in serious trouble.

Brandie

4:29 pm on Jan 10, 2012 (gmt 0)

10+ Year Member



Thanks for the advice. In my rush to get the question posted before leaving for an appointment, I realise that my regex expressions were highly sloppy. Wrist slapped!

Anyway, I've now got the following working but would appreciate comments on whether I've covered everything.

RewriteRule ^fruit/([a-zA-Z\-]+)\.htm$ /handler1.php [L]
RewriteRule ^apartments/([a-zA-Z\-]+)([0-9]+)\.htm$ /handler2.php?N=$1&P=$2 [L]

One further question. While the above commands seem to cover me on the Linux server, they don't on my development machine (Windows/Wampserver) where I need to call [localhost...] as opposed to [mydomain.com...] on the Linux production server. I don't want to have to use different .htaccess files for each. Is there any way of coding this differently so that it will work on both machines, in the way that I can in my php scripts?

Thanks in anticipation of you putting me straight.

lucy24

11:32 pm on Jan 10, 2012 (gmt 0)

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



There's no way around it: leading / means "your current host, whatever it may be" while http:// et cetera means "this specific, named domain".

As a compromise you could include a RewriteBase statement. You'd still have to remember to change or delete it when you move between servers, but you'd only have to change one thing in one place.