Rewrite not work properly - php

My problem is whenever I write
RewriteRule ^ab-c/([^?]) abc.php?test=$1
it works but when I write (no hyphen between b and c)
RewriteRule ^abc/([^?]) abc.php?test=$1
Rewrite Engine is not work properly.

This seems to be a problem of options MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To turn it off place this line at top of your .htaccess:
Options -MultiViews

Related

.htaccess file not rewriting URL's correctly when project is in subdirectory

I currently have a problem with rewriting my URL's using the .htaccess file when a Laravel project is in a subdirectory.
usually when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works.
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
The home page works correctly though any links just return a not found error.
What changes would I need to have the .htaccess file do this for me.
so on investigating further upon entering 'index.php' into the url itself the page loads correctly if this helps anymore with answering the question.
Thanks!
I wouldn't modify the default .htaccess provided in a Laravel app. You need to use Apache's directory alias in the virtual host config file for your site. You need to have the following format:
#Change the paths accordingly
Alias /vacancies /path/to/app/public
<Directory /path/to/app/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted #If using Apache 2.4 add this.
</Directory>
I managed to fix this thank you for your suggestions, This turned out to be a problem with the virtual host file and where the project was created as I have soft links let up to direct apache.
As such Apache was looking in the actual project location set in the virtual hosts file rather than the soft link one intended causing the not found issues.
Check of .ht* permissions in the apache httpd config file if you are using apache. I had the same problem and I had permissions denied problem for .htaccess file. If you are on shared host, try to contact hosting support.
Tried putting it on top of the htaccess file ? Helps for me in some kind of cases, seems weird but it does.
I notice fro your answer you've managed to "fix" this. However, you have some fundamental errors in the .htaccess file you posted, so I'm not sure how this is working exactly?
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
These two directives are not valid in .htaccess files and consequently will result in a 500 Internal Server Error. These can only be used directly in the server config or virtual host context. These are also Apache 2.2 directives, so won't work on Apache 2.4.
RewriteRule ^index.php [L]
(Part of the "front-controller"). You are missing a space between the first and second arguments, so this will fail to rewrite any requests to index.php - your "front-controller". (Maybe this is just a typo, but it's difficult to see a typo like this could creep in?) For example, this should be more like:
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
These directives should be before the front-controller, not at the end of the file. Otherwise, they are not going to execute for any request that is routed through your framework.
when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works
That's not actually what the above .htaccess code does. It should simply be rewriting the request to /index.php. (The frawework/Laravel then looks at the full URL that was requested.) If it rewrote the URL to /index.php/vacancies (that some frameworks do) then you would need to read the URL using pathname information (PATH_INFO).
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
See above regarding the PATH_INFO. But if the .htaccess file is located in the document root of the site and the /abc subdirectory should be entirely hidden then you still have some work to do. You'll need to set the appropriate RewriteBase and modify the condition that removes the trailing slash off non-directories.
Otherwise, if the /abc directory is part of the URL then the .htaccess file can be moved to the /abc subdirectory, but you'll still need to modify the directives that remove the trailing slash.

URL Rewriting and GET values

I know there are lots of answer about this, but I can't understand why it does not work for me.
I want every URL like :
http://mywebsite.com/campagne/blabla
to be redirected to
http://mywebsite.com/campagne.php?c=blabla
Here is my .htaccess file:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule campagne/(.+)$ campagne.php?c=$1 [QSA,L] [L]
The URL is redirected to campagne.php but $_GET['c'] is not sent.
Can you help me? I cannot find what is wrong? It works on my local server but not online.
This is due to enabling of MultiViews option in your Apache. Place this line on top of your .htaccess to turn it off:
Options -MultiViews
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

how to use multiple url RewriteRule in .htaccess

this is working
RewriteRule ^([a-zA-Z0-9_-\s]+)-([a-zA-Z0-9_-\s]+)\.html$ detail.php?category=$1&model=$2
this is working
RewriteRule ^([a-zA-Z0-9_-\s]+)\.html$ brand.php?brand=$1
this is not working
RewriteRule ^([a-zA-Z0-9_-\s]+)\.htm$ colour.php?colour=$8
only two rule working how to use multiple rewrite rule using .htaccess
if i use like this ^colour/([a-zA-Z0-9_-\s]+)\.htm$ colour.php?colour=$1 then it is working with out colour/ this it is not working
This problem seems to be due to enabling of MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To disable this use this line at top of your .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^colour/([\w\s-]+)\.htm$ colour.php?colour=$1 [L,QSA,NC]

Trouble with php mod_rewrite

i'm having some mod_rewrite problems. I am working on a rewrite simple rewrite tool that should help me with my website. but i don't know where is the problem
this is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^text\/$ text.php [L,NC]
and my phpinfo() says that i have my mod_rewrite loaded
also i have a apache hadler: AllowOverride On
so I don't know why this isn't working... can anyone tell me what else could be the problem ?
You need to add the following at the top of your .htaccess file
Options +FollowSymlinks
Also, what rewrites do is allow you to go to
http://www.mydomain.com/text
and see the content that would be generated if you actually went to
http://www.mydomain.com/text.php
rather than the other way around. So the rewrite rules are basically
RewriteRule <what-I-type-in-my-address-bar> <what-page-I-see>

How do I stop URLs that don't exist from resolving to a file?

I don't know if this is an Apache or PHP thing but if you go to either of:
mysite.com/about
mysite.com/about/
it will show the mysite.com/about.php file instead of a 404 error, because the directory does not exist.
I am sure this is some setting, maybe an htaccess thing, any ideas?
As others have answered it might be a mod_rewrite rule; there is also a directive called MultiViews that is explicitly for enabling the functionality you describe.
http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews
The effect of MultiViews is as
follows: if the server receives a
request for /some/dir/foo, if
/some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the
server reads the directory looking for
files named foo.*, and effectively
fakes up a type map which names all
those files, assigning them the same
media types and content-encodings it
would have if the client had asked for
one of them by name. It then chooses
the best match to the client's
requirements.
Look for
Options MultiViews
in your httpd.conf or virtualhost-specific config file.
That's not default behavior, for sure, so it's hard to say. If that were happening on the Apache side, look for a sequence of rewrite matching instructions; they'll likely begin with RewriteCond and ending with RewriteRule.
If you don't see anything like that in your VirtualHost config or site .htaccess, it's being managed by whatever PHP framework you're using, and you'll need to configure things there instead.
with this .htaccess you can parse, with $_SERVER[REQUEST_URI] in the PHP side, the complete URL and then perform your desired action, this is a general way, you should consider error controlling and 404 redirection:
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and int the PHP side, that should be this index.php
$URLarray = explode('/', $_SERVER['REQUEST_URI']);

Categories