Apache - using .htaccess redirect to root folder - php

I have a problem with the .htaccess file. I have it configured in this way.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/$ index.php?controller=$1 [L]
Every time I access http://localhost/mvc3/contacto/ I'm accessing http://localhost/mvc3/index.php?controller=contacto, up there it is correct but if the friendly URL I remove the final character / (http://localhost/mvc3/contacto) it returns me to the root page (wampserver server).
Here is explained in video of the problem, what would be happening?
Video ▶️ http://recordit.co/FfXHww1xCv

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?controller=$1 [L]
Remove slash from the rewrite rule and it should work.

may below changes in .htaccess will be helpful for you
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ index.php?controller=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>

Related

How to change domain name by .htaccess

I have a domain eg: abc.com. While displaying news the url will be abc.com/news.php?news=xyz. And i want to change the url to something like news.abc.com/xyz.
Is it possible via .htaccess
use this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !^www.site.com [NC]
RewriteCond %{HTTP_HOST} ([^\.]+).site.com [NC]
RewriteRule ^(.*)$ %1.php?%1=$1 [L,NC]
</IfModule>
use this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ news.php?news=$1 [QSA,L]
</IfModule>

Apache mod_rewrite problems

I have a project with the following in the .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,NC,QSA]
I have two files in a directory test.txt & test.php
I can access /path/to/folder/test.txt but not /path/to/folder/text.php.
What am I missing? This configuration did work before on a different server.
Try this...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.php [L,NC,QSA]
</IfModule>

Php apache mod rewrite remove .php not working

I'm trying to remove .php from my url,
below is my .htaccess contents
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ path.php?username=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
I don't have any idea what's going wrong.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You need an additional rule to redirect .php URL to non-php one. Try this code:
Options -MultiViews
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ path.php?username=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \s/+folder/(.+?)\.php[\s?] [NC]
RewriteRule ^ %1? [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

.htaccess returns 500 for a simple redirect in Laravel

I have created an app in Laravel.
My initial .htaccess was:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And it worked properly for the domain api.viglug.org.
After I added the m.forum.viglug.org domain (with the same root directory of api.viglug.org).
At this point was exactly the same to call api.viglug.org and m.forum.viglug.org.
I wanted to use the m.forum.viglug.org for the folder api.viglug.org/mobile so I thought it would have worked with this .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} m\. [NC]
RewriteRule ^(.*)$ index.php/mobile/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I was wrong. It keeps returning error 500 if I use m.forum.viglug.org, while api.viglug.org works as expected.
How can I fix this?
The original rewrite only runs on files which do not exist as real files or directories, due to these two conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Have you tried adding those two conditions above your additional rule?
# Mobile site
RewriteCond %{HTTP_HOST} m\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/mobile/$1 [L]
# Normal site
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

mod_rewrite not recognizing files

My mod_rewrite is working excellent, but it's not recognizing anything after for example /calgary/
So, if I go to /calgary/login.php and it is only seeing the index.php page? It won't recognize the /login.php page?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>
You need to swap those 2 rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
# This used to be the 2nd rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
# there used to be a "?" here, remove it ----v
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]
# this used to be the first rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
</IfModule>
The less restrictive rule (the one that matches (.*)/?$) will match /calgary/login.php outright before the second set of rules gets to do its thing, which seems to be rewritten to /city_name/?login.php?page=calgary.
Is that really what you want? There are two ? in the target there. Maybe you only want /city_name/$2.php?page=$1?
Try removing the 'L' from the first rewrite rule, this tells Apache to stop processing further rules if a match is found.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>

Categories