.htaccess all request to other file - php

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 :)

Related

Removing File URL with .htaccess

I am writing a program and run it locally with xampp. the full path to the program is http://192.168.101.103:7777/WeltesMaft/index.php
I am writing a .htaccess in C:\xampp\htdocs\WeltesMaft\
containing this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://192.168.101.103:7777/WeltesMaft/%1 [L]
but somehow my htaccess doesnt work. it still showing like this
http://192.168.101.103:7777/WeltesMart/_resources/main.php
which is in index.php i am including main.php. So when user enters index.php, it will redirect to main.php in an instant.
I want to hide it so that the url looks like this,
http://192.168.101.103:7777/WeltesMart/
Please help me what am i doing wrong here...
You can use this code also.....
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _resources/main.php/$1 [L,QSA]
</IfModule>
Change RewriteBase to your directory name and change the htaccess rule as following and try again:
RewriteEngine On
RewriteBase /WeltesMaft
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule / index.php [L]

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]

.htaccess not working with some PHP files

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]

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.

.htaccess - Redirect subfolder to index.php using subfolder name as variable

This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]

Categories