.htaccess not working with some PHP files - php

I have a .htaccess file that wont find some php files but will find others. Example,
RewriteRule ^test$ test.php
will give a 404 not found but
RewriteRule ^custom$ test.php
will work.
Its the same for a rule that will add .php to the end of the URL. Any ideas?
Full file
RewriteEngine On
RewriteRule ^test$ test.php #doesn't work
RewriteRule ^custom$ test.php #works
full directory(ls -a)
. .. .htaccess index.html test.php
Thanks.

Make sure multiviews is OFF so that it's not causing any funny business trying to match files. Also always use the [L] so that it stops processing rules when a rule is met. That can also cause issues down the line by continuing to run other rules. It should not matter if you have a trailing slash or not. For good measure you can check with conditions too so that if it's not a real file or not a real directory it will process the rule and you won't get a 404.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ test.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^custom/?$ test.php [L]

When there exist a file/folder in your root directory with rule name, it wont work as desired. Because entering http://127.0.0.1/test will change it to http://127.0.0.1/test/ directory by default. but not with http://127.0.0.1/custom so add a trailing slash to your rule.
RewriteEngine On
RewriteRule ^test/$ test.php
RewriteRule ^custom$ test.php

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1.php [L,R=301]
This is what I use on my website to add trailing slashes, I modified it to add .php instead. Is this what you are looking for?
or this should work also
RewriteRule ^test test.php [L,R=301]
RewriteRule ^custom test.php [L,R=301]

Related

how to add index.php in the URL through htaccess

Actually I need to add index.php in my application URL through htaccess file.
My URL is like this.
http://localhost:8080/myapp/xyz/abs.html
I need to change this into.
http://localhost:8080/myapp/index.php/xyz/abs.html
Can anyone tell me what i need to be write in htaccess file.
Any Help will be appreciating.
Thanks.
Have this rule in /myapp/.htaccess:
RewriteEngine On
RewriteBase /myapp/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php/$1 [L]
Presumably an internal rewrite is required, not an external redirect? In which case, try something like the following, using mod_rewrite in your root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index\.php/
RewriteRule ^myapp/(.*) /myapp/index.php/$1 [L]
The RewriteCond directive is required to prevent a rewrite loop. (Only rewrite if it doesn't already contain "/index.php/".)
Try this in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /myapp/xyz/([^.]+)\.html [NC]
RewriteRule ^ /myapp/index.php/xyz/%1.html [R,L,NC]

Why does my rewrite rule only work on specific word?

I need a rewrite rule, which rewrites every request to index.php, except when the word web is in the url. So, every image, JS, CSS file is located under the web folder. Here are my .htaccess file which currently does the trick:
<IfModule mod_rewrite.c>
RewriteEngine On
# Folders / files to exclude from rewrite divided by Pipe goes here:
RewriteRule (^|/)web(/|$) - [L,NC]
# turn empty requests into requests for "index.php",
# keeping the query string intact
RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico$
RewriteRule ^(.+)$ index.php [QSA,L]
RewriteRule ^(.+)$ index.php [QSA,L]
</IfModule>
This works for:
http://localhost/~alpham8/application/index/index/
http://localhost/~alpham8/application/
http://localhost/~alpham8/application/index/someaction/
http://localhost/~alpham8/application/web/img/someimage.png
But it does not work for:
http://localhost/~alpham8/application/somecontroller/someaction/
The none-working URLĀ“s returns me an 404 error.
Please help me.

Apache Mod Rewrite For Front Controller Pattern

I have the following in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _service.php?uri=$1 [NC,L]
</IfModule>
Only problem is there is no rewrite happening and I simply see a directory listing when attempting to access the root page of the server localhost.
Even when I try to make the match optional RewriteRule ^(.*)?$ _service.php?uri=$1 [NC,L] it does not work.
The rewrite seems fine for any other case e.g. localhost/foo/bar
How do I get the rewrite to happen for this particular case?
The problem is that the root is a directory. So this condition is preventing your rule from getting applied:
RewriteCond %{REQUEST_FILENAME} !-d
Try adding a rule like:
RewriteRule ^$ _service.php?uri=/ [NC,L]
or something
You might need to specify the document root in order for the file or directory to be found; my variation on this pattern is:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L,QSA,B]
The root page is served correctly (for me) in this case (e.g. http://www.example.com/). NB, I have this in vhost.conf and not in a .htaccess.

mod_rewrite /foo to /index.php?id=foo AND /foo/foo2 to /index.php?id=foo/foo2

I have a problem with mod_rewrite.
I want to redirect those URIs in the title. I use the following rule
RewriteEngine on
RewriteCond $1 !^(folders not to be redirected e.g. css|images)
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php)
RewriteRule ^(.*)$ index.php?id=$1 [L]
It work only if I put all the resources in a folder, else it would tell me:
"/foo/index.php" not found.
So to solve that I put all the resources in the folder "www"
But when I try to load the resources from a subfolder of e.g. "foo" it tells me:
The requested URL "/foo/foo2" was not found on this server.
How can I load resources from a subfolder like "/foo/foo2" or even "/foo/foo2/foo3"?
And how can I solve the problem with the automatic search for index.php in a folder?
I believe you can use the following to achieve your desired result. It doesn't filter by file extension, but rather checks to see if the file actually exists. One thing that is a little different is that it first appends a trailing slash to your links, something you may not want.
RewriteEngine on
RewriteBase /
# This appends a trailing slash. You will have to update http://so with your domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://so/$1/ [L,R=301]
# This does the internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /index.php?id=$1 [L]
If you don't want the tailing slashes, you could use the following
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?id=$1 [L]

.htaccess all request to other file

Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)

Categories