.htaccess rewrite rule for same words - .htaccess

I have two rules causing problems:
RewriteRule ^posts$ posts.php
RewriteRule ^posts/create$ postscreate.php
I know that I could use "post/create" but these are just two rules out of more that are having all the same problem.
I worked with this rules perfectly on localhost (XAMPP).
However, once I moved the files to my hoster and visited "sub.domain.com/posts/create" it redirects me to posts.php instead of postscreate.php.
Why does it work fine on localhost but not on my hoster? And how can I fix it?
My full .htaccess looks like this (with the two rules only):
AddType application/x-httpd-php .html
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
Options -Indexes
RewriteRule ^posts$ posts.php
RewriteRule ^posts/create$ postscreate.php
The .htaccess on localhost is exactly the same except the https rules. I also tried the .htaccess without the https rules on my hoster but it still didn't work.

Why does it work fine on localhost but not on my hoster? And how can I fix it?
It is most likely due to option MultiViews being turned on on hoster but turned off on localhost.
To disable this use this line on top of .htaccess:
Options -MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

Related

too many redirects with .htaccess and a very simple configuration

Good day all.
I'm doing a simple dashboard o a site, I've set up a very simple htaccess to handle some URLs:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#routing:
RewriteRule ^login/?$ login.php [L]
RewriteRule ^page/([0-9]+)/?$ /index.php?page=$1 [QSA,NC,L]
#errors:
ErrorDocument 404 /404.php
As far as I know this should be quite straight forward, but:
going on /page/1234 URL works fine (as well as any other number used, so the pattern is working).
going on www.example.com/login OR www.example.com/login/ is causing:
1) too many redirects error if the "errorDocument" line is on top of everything
2) a /404.php redirect if the "errorDocument" is at the bottom of the htaccess.
while, I can access directly /login.php without any problems.
i've done some tests but I can figure out what is going wrong, I've also tested the file with this tool:
Converting my comments to answer.
Looks like you have MultiViews option turned on and getting this unexpected behavior.
Options -MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will try to resolve it and serve /file.php.

.htaccess second RewriteRule note working

I have a PHP page like example.com/new/info.php?title=example. The .htaccess is in folder /new/. I tried it in the main directory also. My .htaccess looks like this:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?title=$1 [QSA,L,NC]
RewriteRule ^info/([a-zA-Z0-9_-]+)/$ info.php?title=$1 [QSA,L,NC]
The first Rule is working, but the second sends no GET['title'] to the server. The site info.php loads but without the variable. I have tested it on my localhost and it's working. I load it on my webspace and the second rule is not working.
I tried it also without new directory example.com/info.php?title=example and same not work.
What is my mistake?
It sounds like you have MultiViews enabled (possibly in the server config). You need to disable MultiViews for this to work. Add the following at the top of your .htaccess file:
Options -MultiViews
With MultiViews enabled, mod_negotiation will issue an internal subrequest for info.php when making a request for /info/example/ - before your mod_rewrite rule is processed, so no parameters are passed.

Symfony .htaccess rewrite and redirect not working

I know very little about symfony framework. I copied a website www.example.net to www.example.com site works with url www.example.com/www/app.php or even with www.example.com/www/ but I want it to redirect automatically to app.php without showing it in the url. My htaccess in the www (web) directory is as follows:
&ltIfModule mod_rewrite.c&gt
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
&ltIfModule&gt
It doesn't redirect.
Thanks
In your Vhost:
Change this line (Directory section):
AllowOverride None
to:
AllowOverride All
Instead of that rule can you try:
RewriteBase /www/
RewriteRule ^((?!app\.php).*)$ app.php/$1 [NC,L]
Sorry I can't test with Symphony as I am not near my home computer.
A bit late to this game but I just found an Apache directive that saves so much time and makes your htaccess cleaner. The only caveat is you must have Apache 2.2.16+. To have all urls except valid files (images, etc) use app.php as a front controller use the FallbackResource directive.
<Directory "/web/symfony-project">
FallbackResource /app.php
</Directory>

Url not complying to .htaccess rules

I have a problem with one of the url's in my website , it doesn't seem to comply to my .htaccess rules. The problem I'm experiencing is the following.
I access my site using url: http://www.website.com/sitemap
I have the following .htaccess rules:
ReWriteEngine On
php_value include_path ".:/Webserver/blabla/boo"
RewriteCond %{REQUEST_URI} !^/cms[A-Za-z0-9\-_/]*$
ReWriteRule ^([A-Za-z0-9\-_/]*)$ /site/index.php/
ReWriteRule ^$ /site/index.php/
So I would expect this URL call to rewrite to /site/index.php but the point is it never goes there and instead opens sitemap.xml which is in the webroot. It does work for all other URL's in my site. What could be the case here?
I found the culprit. It was due to the MultiViews option set in httpd.conf and each individual site .conf file. I disabled the MultiViews option in the per site .conf file setting it to -MultiViews
Maybe this question fits better in Serverfault. Is there a posibility for me to transfer it?

.htaccess help - not working

I want to rewrite all .php into .html,,, so i created a .htaccess file and added
AddHandler application/x-httpd-php .php .html .htm
but when it seems not working...
here i uploaded all files - http://www.fellowindian.com/ca/index.php & http://www.fellowindian.com/ca/page1.php
1: Do you have mod_mime installed on Apache.
2: Are you sure that .htaccess is executing.
Simple test would be to see if it can rewrite your urls to add / remove www from it.
Example:
Options +FollowSymLinks RewriteEngine
On RewriteBase / RewriteCond
%{HTTP_HOST} !^www\.mycee\.com$ [NC]
RewriteRule ^(.*)$
http://www.mycee.com/$1 [R=301,L]
3: Which user is the owner of the .htaccess file and what is its attributes?
4: Check that the AllowOverride directive is set in your apache config and not set to None.
Test by putting invalid directives in the .htaccess file and reloading the page.
If the apache error log doesn't show any errors, it's not executing.
5: If you're on shared hosting, check with your host if they have AllowOverride enabled or not.
Personally, I think the best place to put the AddType directive would be in apache's httpd.conf as .htaccess puts a performance hit on your server, but in the case of shared hosting, .htaccess is usually the only option available.
If you're trying to redirect something.php to something.html, you could do
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule (.*?).php $1.html

Resources