25

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
  • 40,709
  • 21
  • 93
  • 115
wesside
  • 5,622
  • 5
  • 30
  • 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

38

This was my solution:

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

Kev
  • 118,037
  • 53
  • 300
  • 385
wesside
  • 5,622
  • 5
  • 30
  • 35
  • This rule is not working for me. While I added your rule, The page will redirect to the welcome page of `xampp`. – Tek Kshetri Apr 07 '20 at 14:37
30

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,774
  • 10
  • 66
  • 83
  • 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? – Tek Kshetri 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. – rockstardev May 21 '20 at 09:31
20
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,743
  • 1
  • 20
  • 33
2

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

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

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

CodAIK
  • 715
  • 7
  • 7