.htaccess file to implement multiple redirects

by RobMorta   Last Updated October 10, 2019 12:04 PM

I have a dynamic site and from .htaccess file creating clean URLs:

RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule ^([a-zA-Z0-9_\-\+\ ]+)$ flight.php?flights=$1&slug=$1

This code worked fine for me but when I created a new type of page and trying to get clean URLs with the same code i.e.:

RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule ^([a-zA-Z0-9_\-\+\ ]+)$ manual-page.php?url=$1&slug=$1

it's not working and if I comment the previous two lines then its is working fine.

Only one code is working at a time.

For first I have a URL domain.com/flight.php?flight-san-fransisco-london-flights and I want this being redirect to domain.com/san-fransisco-london-flights & from the second one I have domain.com/manual-page.php?url=my-new-page and I want this being redirect to domain.com/my-new-page.

Is these any way to get both working together?



Answers 2


Your two RewriteRules match the exact same set of URL paths, and have the exact same RewriteCond, so it should be no surprise that the first one catches any matching paths and leaves nothing for the second one.

To fix this, you'd first need to decide how you're going to tell the two sets of URLs apart. That is, when the user's browser requests http://domain.com/my-new-page, how will your webserver know that this request should be handled by manual-page.php and not by flight.php?

Depending on how you decide to do that, you have a couple of options on the actual implementation:

  • If the difference can be expressed as a regexp, you can just change the first RewriteRule so that it won't match any URL paths that should be handled by the second one. For example, if all slugs for flight pages containg the word "flight", and none of those for the other pages do, the you could replace the first RewriteRule with:

    RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
    RewriteRule ^([a-zA-Z0-9_\-\+\ ]*flight[a-zA-Z0-9_\-\+\ ]*)$ flight.php?flights=$1&slug=$1
    
  • Alternatively, you could have all requests first handled by, say, flight.php, and modify that script so that it calls manual-page.php if it can't find any matching page to display. Or you could write a single front-end script that takes all requests, checks a database to see which group they belong in, and calls the appropriate script to handle them.

  • Finally, if the scripts do more or less the same thing, with just minor differences, you might want to just combine them into a single script that can serve both kinds of pages.

Ilmari Karonen
Ilmari Karonen
July 01, 2013 07:03 AM

I want to allow clean URLs in the form of domain.com/urlname/me@msn.com which should redirect to domain.com/urlname

Sayali Mahadik
Sayali Mahadik
October 10, 2019 11:47 AM

Related Questions


Updated March 28, 2015 06:01 AM

Updated April 19, 2015 20:01 PM

Updated May 02, 2015 21:01 PM