Farshad H.
Farshad H.

Reputation: 307

.htaccess Redirect all url requests to single page with query

I want to redirect any url request to my "index.php" page, plus a query string. like this:

if user typed "mydomain.com/foo" in address bar, htaccess redirect it to "index.php" but in php file $_SERVER[] variable contain "foo" string

in other word i need any string after "mydomain.com/" save in a variable in "index.php" file.

another examples:
mydomain.com/new/bar ----> "new/bar"
mydomain.com/search?color=15 ----> "search?color=15"

is it possible with htaccess or must try another way?

Upvotes: 1

Views: 2537

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use this rule to rewrite all requests to the index.php file

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]

and then in the index.php file you can use the following php code to get the requested uri :

echo $_SERVER["REQUEST_URI"];

Upvotes: 1

Related Questions