Rewriting all request to index.php in the same diectory - php

I want all the request to be rewrited to index.php so I used this htaccess code.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
It works fine when site is hosted on home directory. But suppose if I move my site to a subdirectory xyz and move all my files including .htacess file inside this directory. Then if I access http://example.com/xyz/some_page, the request is not redirected to /xyz/index.php.
So, How can I make this rewrite work even on subdirectories as in my case.
Update:
I forgot to mention the directory xyz as in my case is likely to change frequently. So, this directory doesn't need to be hard coded in the rewrite rule

Remove the RewriteBase
If you don't need the rewrite base - just remove it. A working example based on the one shown in the question would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Note that the first rewrite rule has been deleted as it is logically equivalent to RewriteCond %{REQUEST_FILENAME} !-f (do not rewrite requests for a file that exists).
Consider also deleting RewriteCond %{REQUEST_FILENAME} !-d as this prevents urls like /this/folder/exists being sent to index.php. Most relevant if Directory listings are enabled but not desired.

change your base url for rewrite
RewriteBase subdir/
or try with full url
RewriteBase http://www.examle.com/subdir/

Related

.htaccess redirect all urls to public/index.php without backslash on localhost

Background
I'm creating a time tracking app with PHP on my localhost (MAMP). The app structure is as follows
htdocs/time-tracker/public/index.php
Issue
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
What I need
I want the following result. When I visit the url 127.0.0.1:8888/time-tracker on my local machine, I trigger the php app, routing all requests through the htdocs/time-tracker/public/index.php. Preferably without a trailing slash, but priority is just to get the app to work.
My current .htaccess file
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
Updates
1. $_GET parameters change outcome
For some reason http://127.0.0.1:8888/time-tracker?debug=true and http://127.0.0.1:8888/time-tracker get me different results.
http://127.0.0.1:8888/time-tracker?debug=true results in a redirect to http://127.0.0.1:8888/public
http://127.0.0.1:8888/time-tracker results in a redirect to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
Neither of these results are what I want.
2. Partially working
This .htaccess file has gotten my redirects to work whenever I put something in the $_GET
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L]
For example, 127.0.0.1:8888/time-tracker/?test=test works while 127.0.0.1:8888/time-tracker/ still redirects to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
3. Not redirecting properly on root
The redirects works on all paths except for the root path. For example, 127.0.0.1:8888/time-tracker/test and 127.0.0.1:8888/time-tracker/?test=test both work, just not 127.0.0.1:8888/time-tracker/
I don't know why my regex won't pick this up. Here is my code
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php [L]
4. Seeing an empty path name
I've tracked it down to one last issue: empty paths don't register with the redirect.
# Settings
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine On
# Rules
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*|x) index.php?/var=$1 [L]
For some reason, it just can't catch the redirect if the path is empty.
5. Close enough solution
This is the best I got. This is actually working, but I couldn't fix the trailing slash issue.
# Settings
DirectorySlash On
RewriteEngine On
# Rewrite
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?/var=$1 [L]
Hopefully somebody can come solve the trailing slash directory root issue or at least confirm that it is impossible. At this point, the correct answer goes to anyone who can explain my mistakes and make this into a helpful post.
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Try this. Put your .htaccess file in the time-tracker folder
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

URL Rewrite not working on .htaccess

I have implemented my small website on a live server, however I want to use url-rewrite for some links to make it clean. I try this on my localhost server:
http://localhost/cb/2/login.php to http://localhost/cb/2/login
I manage to make it work on my localhost server by editing the httpd.conf of apache and enable url-rewrite. Here's the rule I added to apache:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/cb/2/login$ /cb/2/login.php
</IfModule>
However, when I upload my files to a live server, obviously I do not have privilege to edit httpd.conf of that server, so I just put my rewrite rule on .htaccess file and here's the content of it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/login$ /login.php [L,QSA,NC]
But when I try to test it, 404 not found is the response of server. I also try to implement in on my localhost server assuming I cannot edit the apache config and same error occurs, Object not found!
I have no idea what will I do next. I'm new to this url rewrite rule so any help will be much appreciated :)
UPDATE
After a lengthy discussion with the OP it seems there is a lot more going on behind the scenes.
The end result is no matter what is placed in the .htaccess file... it doesn't function.
OP was advised to contact the web host, but I will leave my original answer below for others on the site.
Remove the forward slashes from your rewrite rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cb/2/login$ cb/2/login.php [L,QSA,NC]
You changed your rewrite rule used on localhost when you put it on a live server. Assuming your directory structure is the same you should keep your rewrite rule the same (minus the forward slashes).
Remember in rewrite rules...
The text between ^ and $ is what the visitor will input into the url bar, is what your link will be and is what the visitor will see in the url bar.
The next block is the location where the actual file exists.
UPDATE
If you want your visitor to go to www.examplewebsite.com/login.php and you want the url to look like www.example.com/login... your rewrite rule would be simply...
RewriteRule ^login$ login.php [L,QSA,NC] # Handle log-in
Now... let's say that your login.php is inside another directory called admin...
RewriteRule ^login$ admin/login.php [L,QSA,NC] # Handle log-in
These two lines mean...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
If the url is not a "real" directory or the url is not a "real" file... then process the rule.
That's why using RewriteRule ^login$ index.php causes your url to be rewritten as www.examplesite.com/login.php because the rewrite rule cannot be processed (index.php could not be found), BUT login.php is a "real" file.... so it goes to it.
please try:
Options All -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php

.htaccess rewrite to index.php infinite loop

I am building a clean url system using .htaccess and php.
The main idea is to rewrite everything to index.php and to split the url to segments separated by /.
It works but when I upload it to web server I get an infinite loop error.
My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If I get things correctly - the problem is that it redirects index.php to itself but it shouldn't because I have index.php file that handles everything and RewriteCond %{REQUEST_FILENAME} !-f which should prevent that rewrite.
Could you please help me with this .htaccess - I want it to rewrite everything to index.php and to be as portable as it can be, so I can use it in:
www.mysite.com
www.myothersite.com
or even
www.mysite.com/new/
I also want to redirect something like www.mysite.com/articles/2 to index.php as well as www.mysite.com/articles/it/2 or even www.mysite.com/articles/it/search/searchterm
Here's code taken from Drupal's .htaccess which uses the "clean urls" approach you are trying to achieve.
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In this case, it appends the clean URL as the "q" querystring parameter. So then your script can detect what content to deliver based on $_GET['q'].
And here's another approach, this one from Wordpress.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This one specifically looks for requests to index.php and filters them out before doing the redirection.

Keep URL the same, load index.php

My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!

Redirecting even absolute url with mode rewrite

i am trying to implement pretty url.
Everything i have written in PHP and it works properly. I need to handle every request by index.php.
For example website.com/page/search/question/dogs will be handled by index.
BUT website.com/templates/header.tpl will start downloading the file.
I want to handle by index.php even adress to another file.
In .htaccess i have.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
How can i hande even absolute url to file by index.php?
If you want to rewrite URLs for files that exist on the server already, you need to drop the !-F (not a file) and the !-D (not a directory) check. Otherwise you've setup the wrong RewriteConditions for your RewriteRule.
However, there is one caveat, you don't want to rewrite for index.php itself, otherwise it would result in an endless rewrite-loop.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=^./index\.php$
RewriteRule ^.*$ ./index.php [L]
See as well RewriteCond Directive­Docs.
rmove the conditions that the url must not be a directory or a filename:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

Categories