/
.. .htaccess
.. index.php
I have two files shown above in my servers root directory. I want to write a rule in .htaccess so that every time it will pass every parameters (i mean (.*)) to index.php?controller=parameter and index.php will route it accordingly. How can I write such a rule. Thanks
This rewrites everything to index.php except for existing directories/files:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /pro/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
Passing url to controller is not mandatory, since you can use $_SERVER['REQUEST_URI'] (for example) in your index.php
Related
I did some research and spent my last 2 days trying to make my .htaccess work without success, and I can't fully understand how .htaccess really work.
This is the URL I'm trying to rewrite, using GET in my .php files:
http://localhost/BDsite/tables/table.php?table=Energy
and I want it to be like this:
http://localhost/BDsite/tables/Energy
Well, this is how my .htaccess is written, and its located inside the site folder, which is /BDsite
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^BDsite/tables/([^/]*)$ /BDsite/tables/table.php?table=$1 [L]
</IfModule>
Sadly nothing is happening with my URL.
As opposed to per-server rewrites, it is possible to do rewriting inside sections or .htaccess files at the expense of some additional complexity. This technique is called per-directory rewrites.
The main difference with per-server rewrites is that the path prefix of the directory containing the .htaccess file is stripped before matching in the RewriteRule.
A RewriteBase should be used to assure the request is properly mapped.
(source: apache.org - rewrite intro .htaccess files)
So, using your directory structure, and adding the necessary RewriteBase you'd get the following that should work:
.htaccess file in root folder (BDsite)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /BDsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tables/([^/]*)$ tables/table.php?table=$1 [L]
</IfModule>
Project structure:
So now if you request: localhost/BDsite/tables/energy,
you'll get served localhost/BDsite/tables/table.php?table=energy
(check this by var_dump-ing $_GET in file table.php)
So I've been searching for a while now and can't find anything specific on how to create a pretty url / seo / slug url type system WITHOUT sending everything to a index.php or moving things into subfolders.
Basically I'm making a website which you can currently go to urls like movie.php?id=#### / show.php?id=####. Ideally I'd like the url to be movie/#### or movie/id/#### (or down the line slugs of the name that i can use to grab the right one) etc.
Is there a way to do it without having a single index.php router or am I just going to have to rewrite all my files to adhere to this style?
You can create a rewrite rule in .htaccess that routes the movie urls to movie.php as follows:
movie/123:
RewriteRule ^movie/(\d+)$ movie.php?id=$1 [L]
movie/id/123:
RewriteRule ^movie/id/(\d+)$ movie.php?id=$1 [L]
movie/title-of-movie:
RewriteRule ^movie/(\S+)$ movie.php?slug=$1 [L]
movie/title/title-of-movie:
RewriteRule ^movie/title/(\S+)$ movie.php?slug=$1 [L]
combination movie/123/title-of-movie:
RewriteRule ^movie/(\d+)/(\S+)$ movie.php?id=$1&slug=$2 [L]
Edit: added a full .htaccess example for 1 required with up to 2 extra optional parameters with a fallback on index.php if the url is not for movies.
Options -Indexes
IndexIgnore */*
Options FollowSymLinks
AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^movie/([^/]+)/?([^/]*)/?([^/]*)$ movie.php?param1=$1¶m2=$2¶m3=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php [L]
</IfModule>
^ to match from the beginning
$ to match until the end
? for 0 or 1 occurrence
+ for 1 or more occurrences
* for 0 or more occurrences
If the url rule does not match and the file does not exist then it will route the url to index.php, but you can remove that last part if you don't want that.
Yes, assuming your URL structure follows a relatively consistent pattern, you can definitely do this with an .htaccess file and mod_rewrite (without the need for an index.php file, commonly referred to as a front controller).
Here's a really simple example:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*)$ $1.php?id=$2 [NC,L]
This takes an incoming URL like http://example.com/movie/3 and performs a transparent/internal rewrite (so the user doesn't see the URL change) to http://example.com/movie.php?id=3.
Of course, this example could be expanded to handle more parameters, etc. but hopefully this gets you started on the right path. I highly recommend you read the mod_rewrite documentation for more details: http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
I am trying to forward all requests for other index.php files to the root index.php file with the path encoded as a GET variable. Here are some examples of what I mean:
http://my-site.com/this/is/the/path/index.php would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php
http://my-site.com/this/is/the/path/ would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2F
http://my-site.com/this/is/the/path/index.php?v=var would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php%3Fv%3Dvar
How can this be done with the .htaccess file?
EDIT
To be clear, here is what I am hoping to achieve:
If a path is to a file called "index.php" it redirects as long is it is not the root index file. Note, said index.php file could have GET variables that should be included.
If a path ends in a slash, in should redirect as well as long is it is not the root. Again, note said path could have GET variables that should be included.
Otherwise, it should not redirect any other paths.
In summary, only paths to an index.php file (that are not the root) or that end in a slash should be redirected.
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
You can use this .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?path=$1 [L]
</IfModule>
I suggest learning a little about regular expressions in Apache's mod_rewrite.
RewriteEngine On
RewriteRule ^(.*/index.php)$ index.php?path=$1
Note this will only rewrite URL's that end in index.php and leave all other URL's alone, as that's the behavior you specified. If you want to rewrite all URL's to the index:
RewriteEngine On
RewriteRule ^(.*)$ index.php?path=$1
Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.
i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here