Reputation: 33
I want to redirect: https://www.example.com/whatever-part-is-coming-after-the-url to https://www.example.com/index.php?url=whatever-part-is-coming-after-the-url
without changing the request in the address bar.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
If I use this, it works for URL's like https://example.com/foo, but not for https://example.com/foo/bar (all relative urls, like content/styles.css are now foo/content/styles.css, which gives 404 error).
When I add
[R=301]
The redirection is done right, but it shows an 'ugly' URL. Is there a way to have both?
Upvotes: 0
Views: 34
Reputation: 6254
Use the following .htaccess code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule . index.php [L]
What it does is, redirects all paths and parameters to the index.php.
i.e if the domain is example.com
so http://example.com/test -> the request will be taken to the index.php file and even if the path if http://example.com/test/another/test
The request will still be taken to the index.php file.
This is the exact same code used by frameworks like laravel.
In the index.php file you can use:
$request_uri=parse_url($_SERVER['REQUEST_URI']);
$path = $request_uri['path'];
the $path
variable will give you the path user entered.
Upvotes: 0