Home > Software design >  Removing ? from URL after htaccess redirect and rewrite
Removing ? from URL after htaccess redirect and rewrite

Time:05-30

I've made a new site and Googles indexed one of the URLs which Ive since changed so wanted to add a 301 to the new URL. The URLs are SEF URLs so have been rewritten.

Ive managed to create a 301 from the old page to the new with the correct rewritten URL in the browser but the way Ive done it I have a trailing ? at the end of my URL.

Firstly, does this even matter? Will Google rank it as the same page with and without the ? or will it treat the URL with the ? as a separate page of duplicate content?

If its treating it as a separate page then how can I remove this ? at the end?

Here's my rule in htaccess:

RewriteEngine on
Redirect 301 /search/toys/heroes-of-goo-jit-zu https://example.com/search/figures/heroes-of-goo-jit-zu?
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]  
RewriteRule .? https://example.com%{REQUEST_URI} [R=301]
RewriteRule ^search/([a-zA-Z0-9.\-_] )/([a-zA-Z0-9.\-_] )/([a-zA-Z0-9.\-_] ) search.php?category=$1&subcategory=$2&subsubcategory=$3 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_] )/([a-zA-Z0-9.\-_] ) search.php?category=$1&subcategory=$2 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_] ) search.php?category=$1 [QSA]

Without the ? on the end of the rule, I get all the query string on the end of the URL which I don't want. I have tried various flags on the end, both [QSA] and [QSD] cause 500 errors and this is the only method I could find to do the redirect and rewrite without causing 500 errors

CodePudding user response:

The mod_alias Redirect directive will preserve the trailing ? on the target URL. You should use a mod_rewrite RewriteRule directive instead to do as you require and remove the query string. And since you are using mod_rewrite for other redirects/rewrites you should avoid mixing redirects from both modules in order to avoid accidental conflicts. (mod_rewrite is always processed first, despite the apparent order of directives in the config file.)

Using mod_rewrite instead:

RewriteRule ^search/toys/heroes-of-goo-jit-zu$ https://example.com/search/figures/heroes-of-goo-jit-zu [QSD,R=301,L]

The QSD (Query String Discard) flag removes the original query string from the redirect response.

I've included the end-of-string anchor ($) on the RewriteRule pattern, although strictly speaking it should be omitted to be equivalent to your original Redirect directive (which is prefix-matching).

You will need to clear your browser cache before testing since the earlier 301 will have been cached. (Test first with 302 - temporary - redirects to avoid potential caching issues.)

You are also missing the L flag on all your other rules - this should be added. At the very least, this prevents unnecessary processing of later rules, but with the L flag missing on the canonical redirect, this could result in an erroneous redirect, exposing the rewritten URL.

UPDATE: To clarify the discussion in comments regarding the QSA flag. This has nothing to do with your immediate issue. You will need the QSA flag if you are expecting a query string to be past on the requested URLs (in order to merge the query string with that stated in the substitution string), otherwise, this flag should be removed.

I have tried various flags on the end, both [QSA] and [QSD] cause 500 errors

The flags parameter is only available to mod_rewrite (RewriteRule and RewriteCond directives) - this is not valid on mod_alias Redirect (and RedirectMatch) directive(s).

  • Related