1

I need my site to redirect all URLs with a parameter to a clean path:

Have:

http://example.com/?id=1

Want:

http://example.com

Why this? I am using the share and like facebook buttons into my single page site, when you came from a link with parameter the facebook button doesn't show the cumulative likes (it treat this like a new page). So I need to redirect to clean root path to show them.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
osk386
  • 424
  • 3
  • 13
  • I dont need the parameter I just use it to fill my sitemap – osk386 Jan 04 '17 at 17:52
  • "I just use it to fill my sitemap" - What do you mean by that exactly? You shouldn't include URLs in your XML sitemap that are redirected? – MrWhite Jan 04 '17 at 18:00
  • "You shouldn't include URLs in your XML sitemap that are redirected?" are you asking me or giving me an advice? – osk386 Jan 04 '17 at 19:09
  • Advice really, but I'm asking also... What do you mean by your comment, "I dont need the parameter I just use it to fill my sitemap"? That sounds like you are including the parameter'd URL in your sitemap? Why? You should only include canonical URLs in your sitemap. If you are redirecting this URL then it's not canonical, so should not be in the sitemap. – MrWhite Jan 04 '17 at 21:37
  • Thanks for the advice, your answer works to me. – osk386 Jan 05 '17 at 22:27
  • You don’t need to add redirects - you need to point the like button to the correct URL (which right now it seems you are not doing.) – CBroe Jan 09 '17 at 12:14

2 Answers2

3

Remove any query string from the homepage only

In .htaccess you can use mod_rewrite to redirect all requests that contain any query string. Try the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ /? [R,L]

Like the example given in the question, this only redirects requests that are for the document root / homepage. (ie. not a URL of the form: example.com/something?id=1).

The RewriteCond directive checks that the query string has at least 1 character (ie. the regex .).

The query string is "removed" by appending an empty query string in the substitution string (the trailing ?). The ? is not present in the redirect response.

If you require this redirect to be permanent, then change the R flag to read R=301, otherwise it will default to a 302 (temporary) redirect.


More generic, any URL-path

If you want a more generic solution to remove the query string from any URL-path then you can change the RewriteRule directive to the following (keeping the same RewriteCond directive as above):

RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

Now, a request for example.com/anything?something will be redirected to example.com/anything.

This also uses the QSD (Query String Discard) flag (Apache 2.4) to remove the query string instead of appending an empty query string, as is required on Apache 2.2 and earlier (first example).


Edge case - empty query string

A minor caveat with the directives above, is that a present, but otherwise empty query string (eg. a URL-path with a trailing ?) is not removed. For example, given a request of the form example.com/? then the trailing ? will remain, since the query string is strictly "empty" (and the above condition fails to match).

If you specifically wanted to cache this edge case as well then you could match against THE_REQUEST in the RewriteCond directive instead. For example:

RewriteCond %{THE_REQUEST} \?
RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

The condition simply checks for an unencoded ? anywhere in the URL.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
0

You put a PHP tag, so this is a PHP solution.

In your index.php, you can check if there are any GET variables and redirect back to the site. This needs to be before any output is sent by the page, so it should be as close to the top of the page as possible.

if (count($_GET) > 0) {
    header("Location: " . $_SERVER['PHP_SELF']);
    die(); // Very important to prevent the rest of the page from executing anyway
}

You could also use header("Location: /"); if you want to make sure to hide the name of the script.

yakatz
  • 2,142
  • 1
  • 18
  • 47
  • You should never use `PHP_SELF` unsanitized in script like this. Apart from exposing the name of the script (as you suggest) you are opening yourself up to potential XSS vulnerabilities. – MrWhite Dec 11 '20 at 09:06