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.
htaccess redirect index.html to index.php for domain root only
That rule is probably a bit over-complicated for what you're doing.
You only need:
RewriteRule ^index.html$ /index.php [R,L]
.htaccess: ??/index.php -- ??/, built on a relative path
To serve index.php
from the requested directory you use mod_dir's DirectoryIndex
directive (which is probably already set in the server config, although defaults to index.html
only) - you do not need mod_rewrite for this. For example:
# Serve "index.php" from the requested directory
DirectoryIndex index.php
This instructs Apache to serve index.php
from whatever directory is requested. eg. Request /foo/bar/
then /foo/bar/index.php
is served via an internal subrequest (no redirect). If index.php
is not present in that directory you'll get a 403 Forbidden response (assuming directory listings - as generated by mod_autoindex - are disabled).
To remove index.php
from any URL that is requested directly you can use mod_rewrite. For example:
RewriteEngine On
# Remove "index.php" from any URL and redirect back to the "directory"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+/)?index\.php(/|$) /$1 [R=301,L]
The above will redirect as follows, preserving the requested protocol and hostname:
/index.php
to/
/foo/index.php
to/foo/
/foo/bar/index.php
to/foo/bar/
/fooindex.php
- NO REDIRECT (404 expected)/foo/index.php/bar
(containing path-info) to/foo/
(path-info removed)/foo/index.phpbar
- NO REDIRECT (404 expected)
The (optional) capturing group (.*/)?
contains the part of the URL-path before index.php
. This is then available in the substitution string using the $1
backreference. In the case when /index.php
is requested in the document root, this is empty. When a subdirectory is present then this contains a string of the form subdir/
, including the trailing slash.
If you have no other directives in your .htaccess
file then you don't strictly need the condition that checks against the REDIRECT_STATUS
environment variable. This condition ensures that only direct requests are redirected in the case when you have a front-controller pattern later in the file that might rewrite requests to index.php
.
If you do have other directives in the file then the order can be important. This rule that removes index.php
via an external redirect must go before any existing rewrites, near the top of the file.
Note that this removes index.php
from the URL regardless of whether the requested URL actually maps to a real file or whether the preceding URL-path even exists as a physical directory. So, /<something>/index.php
is redirected to /<something>/
regardless of whether /<something>/
is a physical directory or not. This check can be implemented at the cost of an additional filesystem check - but it's probably not required.
NB: Test first with a 302 (temporary) redirect to avoid potential caching issues. Only change to a 301 (permanent) redirect once you have tested that it works as intended.
UPDATE#1:
These Questions do not provide an answer:
htaccess redirect index.php to root (including subdomains)
- This does not address subdirectories, only subdomains
- This redirects
example.tld/dir/index.php
toexample.tld/
, but I needexample.tld/dir/
Actually, the first question you've linked to does answer your question, with regards to removing index.php
from the requested URL. It does address subdirectories and would redirect example.tld/dir/index.php
to example.tld/dir/
(not example.tld/
as you've stated).
The part of the question that discusses subdomains is a bit misleading as it doesn't really have anything to do with subdomains specifically.
The solution presented in the linked question basically does the same sort of thing as I've done above, except that it arguably matches too much (and not enough). It would incorrectly/unnecessarily redirect /fooindex.php
to /foo
(no trailing slash) and would fail to redirect URLs that contained path-info (which could be malicious). eg. /foo/index.php/bar
would fail to redirect but still serve the contents of /foo/index.php
(unless AcceptPathInfo
had been disabled). Although whether these "errors" would actually cause an issue in your case is another matter.
UPDATE#2:
I have the code exactly in the directory for
example.tld/dir
The code above assumes the .htaccess
file is located in the document root. If the .htaccess
file is located in the directory of where the app is installed then you would need to modify the above like so:
# Remove "index.php" from any URL and redirect back to the "directory"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
The %1
backreference (as opposed to $1
) refers to the captured group in the preceding CondPattern. This naturally includes the full URL-path, so avoids having to hardcode the directory in which the .htaccess
file is located.
This applies to the directory that contains the .htaccess
file and any subdirectories thereof. Note that, by default, this completely overrides any mod_rewrite directives that might be present in the parent .htaccess
file (mod_rewrite directives are not inherited by default).
...including subdirectories, which may have their own .htaccess and index.php.
If additional sub-subdirectories have their own .htaccess
file then this may or may not work depending on the directives used in these sub-subdirectory .htaccess
files and/or how mod_rewrite inheritance is configured.
mod_rewrite directives do not inherit by default. So, if the .htaccess
file in the sub-subdirectory enables the rewrite engine then the above mod_rewrite directives in the parent directory will be completely overridden (they are not even processed).
If, on the other hand, the .htaccess
file in the sub-subdirectory uses directives from other modules then this may not be an issue.
.htaccess - Redirect All URLs + Existing Directories to index.php
The error is with this RewriteRule
RewriteRule /sub/directory/index\.php - [L]
See What is matched?
- In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.
and "Per-directory Rewrites":
- The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.
This means, the leading slash prevents the pattern from matching. Changing it to
RewriteRule ^sub/directory/index\.php - [L]
will fix the problem.
The 500 Internal server error comes from the second rule (in combination with the non-matching first rule).
RewriteRule ^.*$ /sub/directory/index\.php
This will rewrite any request to /sub/directory/index.php
, which in turn will be rewritten again to /sub/directory/index.php
, and so on until the rewrite module gives up and shows a "too many redirects" error or similar.
How to redirect index.php to another page properly via .htaccess?
RewriteRule ^index.php /en/home [R=301]
htaccess redirect index.php to root (including subdomains)
Do this:
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Redirecting index.php?p= to product.php?id= using htaccess
With your shown samples/attempts, please try following htaccess rules. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?p=(\d+)\s [NC]
RewriteRule ^ product.php?id=%1 [R=301,L]
How do I htaccess redirect index.html to index.php and index.php to /
The RedirectMatch
directive matches after the internal redirect that takes the /
request and redirects it to /index.html
, then it get put through the URL-file mapping pipeline again and thus matches your redirect directive.
You can try including a:
DirectoryIndex index.php
to prevent this automatic internal redirect. You should also use the %{THE_REQUEST}
match with the index.html
file like you're doing with index.php
:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1index.php [R=301,L]
Or you can bypass index.php entirely:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
why htaccess redirect to https://example.com/index.php instead the specific sub-url?
This is the code I use to redirect to HTTPS
for Laravel:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Force non-www:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Force SSL redirect:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Related Topics
Sending Signal from Kernel to User Space
Linux - Run Process on Several Cores
Understanding The Getting of Task_Struct Pointer from Process Kernel Stack
How _Libc_Start_Main@Plt Works
Haskell Cabal: Mysterious Missing or Recursive Dependencies
How to Use Performance Counters Inside of The Kernel
Making Strlcpy Available in Linux
In Bash, How to Not Create The Redirect Output File Once The Command Fails
How to Move Old File to Another Folder
User-Space Memory Editing Programs
Why The Process Is Getting Killed at 4Gb
What Do Getresuid() and Setresuid() Do
Using Dma Memory Transfer in User-Space
How to Create a Virtual Device in Linux
How to Read/Write From/To a Linux /Proc File from Kernel Space