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?

1 Answer 1

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 ?
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer @P0IT10n The URL is still not rewritten.
Try adding between RewriteEngine On and RewriteCond %{QUERY_STRING} ^n=(.*)$, RewriteBase /
If still not working, try see if your .htaccess is been read. Use this to try HTACCESS rules
I have updated the question to add extra debugging information. :)
You have used http://example.com/blog/cat/?? what should be the correct output of that one ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.