Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem with empty string

mod_rewrite problem with empty string

         

ezeevivek

3:56 pm on Jan 28, 2012 (gmt 0)

10+ Year Member



Hi,

I have a strange problem, where to generate pages I am using my URL's as:

site/city/city-name/page-number

for that mod_rewrite is:

RewriteRule ^city/(.*)/(.*)$ tagcity.php?tag=$1&pnum=$2 [L]

But now the problem is if I delete the city-name and make it empty like "site/city//page-number" then it starts taking page-number as city-name.

I know its a strange situation but unfortunatly some of my sites pages are indexed in Google as above.

How to detect that its empty so I can generate a 404 page?

Thanks,

lucy24

11:54 pm on Jan 28, 2012 (gmt 0)

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



The problem is that you have .* in the middle of your Pattern. The dot . matches anything in Regular Expressions, including directory slash / and literal period .

What you need to say instead is

([^/]*)/

to capture exactly one directory. If it happens to be empty, like your example "site/city//page-number" then you have two choices:

Your php script can include a line to deal with null values,
OR
You can intercept // forms in htaccess.

Probably the easiest is to replace [^/]* with [^/]+ so empty directories never get processed at all. They should then go to 404 by default and you don't have to do anything more.

In fact, you don't see many cities with one-letter names.* You can express the first directory as [^/]{2,} or whatever your shortest name is.


* Is there a French town called Y? Do you have a page about it?