Redirect all traffic to index.php using mod_rewrite
Try replacing ^(.*)
with ^(.*)$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Edit: Try replacing index.php
with /index.php
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
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.
Use htaccess file to redirect all traffic to index.php and non-www to www
Yes, there is. First of all keep your original rule as it was (slightly modified, see below):
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
After that, add:
RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule ^(.*)$ index.php?path=$1 [QSA]
So very similar to what you already had, except:
- removing flags on the last rule that weren't necessary
- adding a check for the host on the second rule
- removing slash on replacement in second rule that is not part of the match in a .htaccess file
- changing the RewriteCond check a little, including redirecting anything that is not www.example.com rather than just example.com. I recommend pointing ww.example.com and similar to your site so spelling mistakes like that get redirected with it (personally I just point *.example.com to my site in most cases).
Let me know any problems.
Redirect all requests to index.php in a folder if exists .htaccess
You can use this rule in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)[^/]+/?$ $1index.php [L]
Mod_Rewrite: Rewrite URL to index.php but redirect index.php to root
Change your THE_REQUEST
rule to
RewriteCond %{THE_REQUEST} ^GET\ /index\.php(/.*) [NC]
RewriteRule ^ /? [L,R=301]
Related Topics
Avoiding Form Resubmit in PHP When Pressing F5
How to Return Back Twice in Laravel
Fastest Way to Add Prefix to Array Keys
Why Do Changes to Some PHP Files Take So Long to Show on the Live Site
How to Enable MySQLi Support for PHP's Cli
Mysqli Prepared Statements with in Operator
Password_Hash Returns Different Value Every Time
How to Redirect After Download in Laravel
Simple PHP Contact Form with Firebase Hosting
PHP Simplexml: Insert Node at Certain Position
Get All Instances of a Class in PHP
How to Pass Jquery Variables to PHP Variable
Getting Complete Path of Uploaded File - PHP
How to Get Wkhtmltopdf to Execute via PHP
Adding Custom Field to Product Category