1

What I have in my URL is https://www.example.com/?id=987456 I can display all the user data fetched from database using the $id = $_GET['id'];

Now I want to remove just the ?id= so the URL will good.

The required URL will looks like https://www.example.com/987456

NB. I have a lot of other files, folder and subfolders in that same domain so these docs must not affected.

I have to get 987456 value so I can display the user content based on that.

Its better if it can work with .htaccess I'm using LiteSpeed Web Server with PHP 7.4 version Just I don't want the user to see this ?id=

How can I achieve this idea.

Please help me?

Thank you for your help in advance.

Anan
  • 33
  • 7
  • Does this answer your question? [htaccess rewrite rule, remove question mark](https://stackoverflow.com/questions/13761125/htaccess-rewrite-rule-remove-question-mark) – Dilip Patel Sep 04 '21 at 13:45

1 Answers1

1

Two completely different things need to happen. First, you need to externally redirect the browser to show something different in the URL address bar. Second, when the browser resends the 2nd request, the server internally rewrites the query string back. You can't arbitrarily add or remove things in URLs in the wild, as they are locators. You can create a new locator, tell the browser to use this new one instead of the old one, then internally on the server change the new one back to the old one.

See the top part of this answer for an explanation

To make the browser go to the new URL:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]

This takes a request for the URL: http://example.com/?something and redirects the browser to the URL: http://example.com/something

Then you need to internally rewrite it back:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

When the request is made for http://example.com/something, the server rewrites the URI to /index.php?something. This is internal to the server so the browser knows nothing about it and will continue to display the URL http://example.com/something while the server processes the URI /index.php?something.

Dmytro
  • 636
  • 1
  • 3
  • 18
  • I tried by adding these code inside my .htaccess `RewriteEngine On RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+) RewriteRule ^ /%1? [L,R=301]` with `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?$1 [L]` respectively But unfortunately it doesn't work. I have Litespeed web server – Anan Sep 04 '21 at 08:29
  • "First, you need to externally redirect the browser" - First, the OP needs to actually change the internal links throughout their site. The "external redirect" is only for search engines (if the old URLs are indexed) and existing backlinks to the old URLs. Otherwise, it will deliver a bad user experience and potentially be detrimental to SEO. You are also missing the `id=` URL parameter name in the rewritten URL. But otherwise +1, great answer :) – MrWhite Sep 05 '21 at 13:35