Question about .htaccess - for URLs without filename

We are currently in the process of doing two things:

  1. Changing the domain name on our site to a new one
  2. Making all URLs search engine friendly

On the old domain - all links were of the type “index.php?variable=x&variable2=y”.

We now need to forward those to the search engine friendly URLs. That is not a major problem - what we do is:

RewriteRule ^index\.php$ /redirect.php [QSA,L]

and redirect.php then takes care of creating the new URLs - and then does a 301 redirect.

But - some of the URLs - particularly from incoming links are of the type:, i.e. without any filename (index.php for instance). Is it possible to “catch” these with .htaccess - i.e. send these requests to redirect.php as well ?

I can make .htaccess catch… but now when the it contains the question mark…

Any help is highly appreciated…

You want to match a URI of “/” alone, the query string doesn’t matter, just like it didn’t matter when you were matching a URI of “/index.php”

RewriteRule ^/?$ /redirect.php [QSA,L]

Thank you very much - so elegant, so simple.

Unfortunately I discovered it had a somewhat less wanted side effect - when you type in (with or without trailing slash) the rule is applied as well.

Is there any way to avoid this behaviour ?

By the way Dan - I noticed we both have been around since August 2000 - almost 10 years. You have made slightly more posts than me though :wink:

There is no such thing as a domain without a trailing slash. All request URIs begin with a slash. When you don’t type it the browser is adding it for you.

So what is it that you really want to differentiate? What must the URL contain for you to want it redirected?

Correct, the trailing slash is being added automatically - I knew this. My bad.

As mentioned - the site is moving from one domain to another - and is getting search engine friendly URLs at the same time.

At the old domain - some links look like this:

What I want is for that to be sent to - and then 301 redirected with a search engine friendly URL.

This works fine for all old URLs that contained index.php, page.php etc - but not for links that started with /?..

Your first suggestion worked - but unfortunately the main URL would also be sent to redirect.php - meaning when I type in it wouldn’t load the page as usual but be sent to /redirect.php. This I want to avoid.

Thanks for all help so far.

Do they all have the same variable (name) present? If so that can be the thing you look for

RewriteCond %{QUERY_STRING} variable
RewriteRule ^/?$ /redirect.php [QSA,L]

Yes - all have the same variable name - so THIS is a working solution. Thanks a LOT. All rewrite rules work as expected now - and even

I guess I’ll hang around another 10 years…

I hope you do :slight_smile:

I’ll probably still be here