Rerouting all php requests through index.php
Here's what I use (and have used for ages):
<IfModule mod_rewrite.c>
# Redirect /index.php to / (optional, but recommended I guess)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
# Run everything else but real files through index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
</IfModule>
As the comments suggest it will route every request that isn't an actual file to index.php
Redirect all .php requests to .html using .htaccess
You can use the following rules to convert your php
URLs into html
:
RewriteEngine on
#Redirect and rewrite php URLs to html
#redirect /user/foobar/index.php to /user/foobar.html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^user/([^/]+)/index\.php$ /user/$1.html [L,R]
#rewrite /user/foobar.html to /user/foobar/index.php
RewriteRule ^user/([^.]+)\.html$ /user/$1/index.php [L]
The rule #1) triggers when /user/foobar/index.php
is requested and redirects it to /user/foobar.html
. Since the .html
file doesn't exist the second rule maps the .html
request back to original .php
page but your URL remains the same in browser address bar.
redirect every request to index.php and interpret the request
Try adding the following to the .htaccess file in the root directory of your site.
It will send all requests (except for existing files/dirs) to index.php.
The original request will be available in the request
param.
RewriteEngine On
RewriteBase /
#skip existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#everything else goes to index.php
RewriteRule ^ index.php?request=%{THE_REQUEST} [L]
How to redirect all requests to index.php and keep the other GET params?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .* index.php?url=$0 [QSA,L]
You need the QSA in your rewrite rule.
.htaccess for redirect all requests to index.php
This is the .htaccess I use
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
Also, do not use relative links anywhere! href="img/some-image.jpg"
should be changed to href="/img/some-image.jpg"
.
Give that a try.
Redirect all to index.php using htaccess
Your rewrite rule looks almost ok.
First make sure that your .htaccess
file is in your document root (the same place as index.php
) or it'll only affect the sub-folder it's in (and any sub-folders within that - recursively).
Next make a slight change to your rule so it looks something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
At the moment you're just matching on .
which is one instance of any character, you need at least .*
to match any number of instances of any character.
The $_GET['path']
variable will contain the fake directory structure, so /mvc/module/test
for instance, which you can then use in index.php to determine the Controller and actions you want to perform.
If you want the whole shebang installed in a sub-directory, such as /mvc/
or /framework/
the least complicated way to do it is to change the rewrite rule slightly to take that into account.
RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]
And ensure that your index.php
is in that folder whilst the .htaccess
file is in the document root.
Alternative to $_GET['path']
(updated Feb '18 and Jan '19)
It's not actually necessary (nor even common now) to set the path as a $_GET
variable, many frameworks will rely on $_SERVER['REQUEST_URI']
to retrieve the same information - normally to determine which Controller to use - but the principle is exactly the same.
This does simplify the RewriteRule
slightly as you don't need to create the path parameter (which means the OP's original RewriteRule
will now work):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
However, the rule about installing in a sub-directory still applies, e.g.
RewriteRule ^.*$ /mvc/index.php [L,QSA]
The flags:
NC
= No Case (not case sensitive, not really necessary since there are no characters in the pattern)
L
= Last (it'll stop rewriting at after this Rewrite so make sure it's the last thing in your list of rewrites)
QSA
= Query String Append, just in case you've got something like ?like=penguins
on the end which you want to keep and pass to index.php.
How can I force all URI requests to index.php?
If you are using apache you can use .htaccess
file to redirect every request in your index.php
file:
RewriteEngine On
RewriteRule ^/index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
If you are using nginx server you can redirect all request to index.php
file with this code in your .conf
file:
location / {
try_files $uri $uri/ /index.php;
}
Hope this helps you
How to redirect all request to index.php even with .php in the url
I already solved the problem.
First you should comment out this line or remove this line from the snippets/fastcgi-php.conf
file
try_files $fastcgi_script_name =404;
then on your virtualhost config put try_files $uri $uri /index.php;
before the include snippets/fastcgi-php.conf;
on the location ~\.php$
block.
the location ~\.php$
block should look like this:
location ~ \.php$ {
try_files $uri $uri /index.php;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
that should do the trick.
Handle http requests for any page by one php file and redirect from index.html
Considering that users are hitting index.html url from your browser and you want to rewrite and redirect it in backend to index.html if this is the case try.
RewriteEngine ON
RewriteCond %{THE_REQUEST} /index\.html\s [NC]
RewriteRule ^(.*)/index\.html/?$ /$1 [R=301,L,NC]
OR in case your URLs are user friendly URLs and you want to redirect in backend to index.html file then please try following. Considering you don't have trailing slashes, working on solution when trailing slashes are there too.
RewriteEngine ON
RewriteCond %{THE_REQUEST} /index\.html\s [NC]
RewriteRule ^(.*)$ /$1/index.html [NE,NC,L]
Related Topics
Namespace in PHP Codeigniter Framework
How to Get First X Chars from a String, Without Cutting Off the Last Word
Php's JSON_Encode Does Not Escape All JSON Control Characters
PHP Code to Test Pdo Is Available
How to Open a Link in Private Browsing Mode
How to Destroy the Session Cookie Correctly with PHP
Pre-Incrementation VS. Post-Incrementation
Redirect All Traffic to Index.PHP Using Mod_Rewrite
PHP File Upload, How to Restrict File Upload Type
Laravel Eloquent: How to Order Results of Related Models
PHP Gd Create a Transparent Png Image
Using Nulls in a MySQLi Prepared Statement
Are the PHP Preg_Functions Multibyte Safe