I need some suggestion on some htaccess rule. What I want is that all requests in my site will redirect to index.php. But also I need to log what url the user was trying to visit in my databse. So, say if someone try to visit a page in my site(which does not exists) like
example.com/somefolder/somefile.php?somevar=someval&somevar2=someval2
then he will actually be redirected to index.php where I will need to somehow get and log the original full url in my database.
Hope I was able to clear my clear my requirement. Thanks.
Put this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
Original URL is available to you in $_SERVER['REQUEST_URI']
The above answer didn't work in my case so after a research I found this helpful and might be helpful for someone else too in future.
To redirect all requests to index.php
, provide these lines in .htaccess
file
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.php$
RewriteRule (.*) /index.php [R=301,L]
Remember this redirects all requests to index.php
even if its your stylesheet.css or jquery.js
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.