23

Ok, im pretty new at this and I would really appreciate some help, thanks!

How can i rewrite this in .htaccess correctly?

So I have a query string in my url:

 /?url=contact

All i want to do is remove the query string

 /contact

Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all

Amit Verma
  • 39,545
  • 18
  • 87
  • 107
wesside
  • 5,462
  • 5
  • 27
  • 35
  • Also see this (`Htaccess Redirect URL with Query strings`)https://helponnet.com/2019/06/21/how-to-redirect-a-url-with-query-string-apache-htaccess/ – Amit Verma Feb 04 '22 at 20:27

4 Answers4

36

This was my solution:

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Kev
  • 115,559
  • 50
  • 294
  • 378
wesside
  • 5,462
  • 5
  • 27
  • 35
  • This rule is not working for me. While I added your rule, The page will redirect to the welcome page of `xampp`. – Tekson Apr 07 '20 at 14:37
28

Try this:

RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]

To a user on your site, they will see and navigate to this:

http://example.com/contact

But the real page would be something like this:

http://example.com/index.php?url=contact

This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.

random
  • 9,571
  • 10
  • 67
  • 79
  • I added the rule as you suggested in my xampp .htaccess file. When I tried to reload my page, it will redirect to localhost/dashboard page. Is there anything missing? – Tekson Apr 07 '20 at 14:29
  • @Tekson, it's happening to me too and it is due to the /index.php bit. Change it to index.php without the slash. – coderama May 21 '20 at 09:31
18
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html  %1

(or whatever if it's not index.html, index.php, whatever)

You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule

Devin Ceartas
  • 4,651
  • 1
  • 19
  • 33
0

Before: https://example.com/index.php?user=robert

RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]

After: https://example.com/user/robert

Mr. Coderx
  • 281
  • 4
  • 3