2

My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.

  1. First one is based on this question:

    RewriteEngine On RewriteCond %{QUERY_STRING} ^n=1$ RewriteRule (.*) $1? [R=permanent]

After the answer below I modified the .htaccess file:

 ```RewriteEngine On
 RewriteCond %{QUERY_STRING} ^n=(.*)$
 RewriteRule (.*) $1? [R=permanent]```

but it still did not work. Here is the debugging info:

RewriteCond %{QUERY_STRING} ^n=(.*)$   
This condition was met.


RewriteRule (.*) $1? [R=permanent]   
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
  1. Second approach is:

    RewriteEngine On RewriteCond %{QUERY_STRING} !^$ RewriteRule ^(.*)$ $1 [QSD]

None of them seem to work.

I have a few more questions:

  1. Does rewriting only work if the URL is not typed manually?
  2. Can I still access the values of query strings in PHP?
  3. What happens when a user copies the rewritten URL?
Community
  • 1
  • 1
SanJeet Singh
  • 1,291
  • 2
  • 15
  • 28

1 Answers1

0

The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.

And the other questions:

  1. It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
  2. Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
  3. What do you mean ?
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43