Using Rewrite Rule for multiple 301 redirects

January 21, 2016 623 views


FULL DISCLOSURE: I'm VERY new and inexperienced when it comes to doing anything with .htaccess files.

My Question: I have a site that has been programed to dynamically generate copies of a page to fit a location. For instance, "" was set up to make about 100 duplicates that look like this: "" with the city and state dynamically changing with each page.

As you can imagine, this creates tons of duplicate content. I want to consolidate them by 301 redirecting all the pages with a city and state parameter so they point to the original page (

After some research, I was able to find a RewriteRule that helped me do this on a page by page basis, but only with the homepage:

RewriteCond %{QUERY_STRING} ^city=Philadelphia&state=PA$
RewriteRule ^$ [R=301,L]

With all that said, I have a two part question:
1) Is there a way to write this so that it targets subdirectory pages? (I could only get it to do the index)
2) Is there a way I can use a wildcare like (.*) in a single RewriteRule so "" and all it's variations point to the original page (

Thanks so much!

1 Answer

points to (/index.html probably, depending on your webserver config). ?city=Washington&state=DC is called a query string and is passed along with the request to your server, but not as part of the URL itself. So, you can just have an index.html page, and use the query string from there.

If you're using PHP, the variables will be available as $_GET['city'] and $_GET['state']. Don't forget to check if they exist first before trying to access them. For example:

if (isset($_GET['city'])) {
    $city = $_GET['city'];

If you are not using any "backend languages", you can use JavaScript to access the parameters. See this, or maybe this library.

Have another answer? Share your knowledge.