URL Rewrite not working on directory - php

I'm trying to set up a URL Rewrite for my website, hosted on 1&1.
Here is my .htaccess on root directory ( ./ )
AddHandler x-mapp-php6 .html .htm
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^404$ /404.html [L]
ErrorDocument 404 /404
RewriteRule ^login$ /login.html [L]
RewriteRule ^register$ /register.html [L]
RewriteRule ^contact$ /contact.html [L]
RewriteRule ^admin$ /admin.html [L]
RewriteRule ^admin/user$ /administration/user.html [L]
RewriteRule ^admin/user/p/([0-9]+)$ /administration/user.html?p=$1 [L]
There is something strange with it :
if I go to website.com/login it works well (/login.html)
if I go to website.com/admin it works well (/admin.html)
if I go to website.com/admin/user it goes to /admin.html instead of /administration/user.html (like if I
tried website.com/admin)
Even if I change the order or if I delete the line RewriteRule ^admin$ /admin.html [L] and I try to go to website.com/admin or website.com/admin/user I still have the same page (/admin.html) like if the url rewrite was in cache or something.
Anyone has a clue?

This is most likely due to enabling of MultiViews on your Apache host. Disable it by using this line on top of your .htaccess:
Options -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.

Related

htaccess php GET not working

My .htaccess doesn't work with GET. It displays "news/", but once I go "news/1" or "news/1/" it doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ index.php [NC,L]
RewriteRule ^news/$ news.php
RewriteRule ^news$ news.php
RewriteRule ^news/([a-zA-Z0-9_-]+)/$ news.php?id=$1 [L,QSA]
RewriteRule ^news/([a-zA-Z0-9_-]+)$ news.php?id=$1 [L,QSA]
My PHP is:
if(!isset($_GET["id"])){
$Article->printArticles();
}else{
$Article->printArticle($_GET["id"]);
}
But it doesn't find the $_GET["id"] but at localhost it works.
Have tried with [L,QSA], [N], [NC,L] and without any of these.
How shall the htaccess look to find the $_GET["id"]?
How shall the htaccess look to find the $_GET["id"]
This is most likely result of MultiViews option being ON on your web server.
Turn it off using this line at the top:
Options +FollowSymLinks -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 if /news is the URL then Apache will serve /news.php.

Mod_rewrite not working for some rules unless first letter capitalized? HTTPS Server

I've been working on this site for quite awhile. Im employing a .htaccess file to get clean URLS. This worked fine on my personal test server, which is http, and on my localhost, but after moving to the production server (with HTTPS enabled and working), some of the rules aren't working
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^index/?$ index.php [NC,L]
RewriteRule ^home/?$ index.php [NC,L]
RewriteRule ^main/?$ index.php [NC,L]
RewriteRule ^about/?$ about.php [NC,L]
RewriteRule ^music/?$ music.php [NC,L]
RewriteRule ^shows/?$ shows.php [NC,L]
RewriteRule ^blog/?$ blog.php?page=1 [NC,L]
RewriteRule ^blog/([\0-9]+)/?$ blog.php?page=$1 [NC,L]
RewriteRule ^contact/?$ contact.php [NC,L]
RewriteRule ^profile/?$ profile.php [NC,L]
RewriteRule ^manage-site/?$ manage-site.php [NC,L]
RewriteRule ^my-blog-posts/?$ my-blog-posts.php [NC,L]
RewriteRule ^new-blog-post/?$ new-blog-post.php [NC,L]
RewriteRule ^edit-music/?$ edit-music.php [NC,L]
RewriteRule ^admin-login/?$ admin-login.php [NC,L]
RewriteRule ^admin/?$ admin-login.php [NC,L]
RewriteRule ^forgot-password/?$ forgot-password.php [NC,L]
RewriteRule ^password-reset/?$ password-reset.php [NC,L]
RewriteRule ^blog/article/([\w-]+)/?$ blog-post.php?slug=$1 [NC,L] #handle requests for Individual Blog Posts
RewriteRule ^blog/article/([\w-]+)/([^/\.]+)/?$ blog-post.php?slug=$1&reply-comment=$2 [NC,L] #handle requests for Individual Blog Posts with a comment specified for replies when JavaScript is disabled
RewriteRule ^password-reset/([^/]+)/?$ password-reset.php?token=$1 [NC,L] #handle requests for password-reset.php with the token included in a nicer-looking url
</IfModule>
ErrorDocument 404 /404.php
Basically any of the rules that has a hyphen, i.e manage-site, gives me a 404 error UNLESS, I capitalize it, i.e Manage-site -- That works fine.
Any ideas?
This could happen if MultiViews (mod_negotiation) is enabled for this directory. Try disabling MultiViews by adding the following to the top of your .htaccess file:
Options -MultiViews
With regards to the code in the question, a scenario in which this might fail (ie. result in a 404) is when you are requesting a URL with a trailing slash and a PHP file exists with the same basename and AcceptPathInfo is Off. eg example.com/main/ should otherwise rewrite to /main.php, but instead gets rewritten to /main.php/ (by mod_negotiation) which results in a 404.
MultiViews is often enabled by default on many servers, however, a default Apache install should not have this enabled out of the box.
With MutliViews enabled, Apache tries to map a non-existent file with a file on the filesystem by testing various file extensions (that would return the appropriate mime-type). eg. Request /main (which doesn't exist) it will try /main.php - success. However, this runs before mod_rewrite, so if MultiViews kicks in, the mod_rewrite rule will never match.
In the scenario mentioned above, /main/ also triggers mod_negotiation, internally rewriting the request to /main.php/ (the trailing slash is still trailing). But if AcceptPathInfo is Off this will trigger a 404. This prevents mod_rewrite from rewriting the URL.
By capitalising the request, MultiViews fails (I assume you are on a case-sensitive OS, eg. Linux?), but since you have the NC flag on the RewriteRule this now works.

Getting 404 error when attempting to use Apache rewrite on dokuwiki

I am building a wiki using dokuwiki, and am using the nice URL feature which uses url rewriting through Apache on the webserver, to get rid of the ugly php urls. My problem is that I seem to be only getting 404 Not Found errors when searching for the pages using the new URLs.
This is the .htaccess file provided, and the RewriteBase is pointed to the correct location.
#Options -Indexes -MultiViews +FollowSymLinks
## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
Order allow,deny
Deny from all
</Files>
## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
RewriteEngine on
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
## Not all installations will require the following line. If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
RewriteBase /dokuwiki
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Make sure your Apache actually loads .htaccess files. You will probably need this in your Apache config:
AllowOverride All

url rewrite working on local but not on server

RewriteEngine on
Options +FollowSymlinks
RewriteRule ^index\.html$ index.php [L]
RewriteRule ^gallery/([0-9]+)/([a-zA-Z0-9-_]+)/$ gallery.php?pid=$1&urln=$2 [L]
On localhost pid=$1 is working perfectly fine but on server (OVH) I get an empty value. The url is working fine, I reach gallery.php
Thanks for your help !
Most likely your server has MultiViews option enabled. 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.
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^index\.html$ index.php [L,NC]
RewriteRule ^gallery/([0-9]+)/([\w-]+)/$ gallery.php?pid=$1&urln=$2 [L,QSA]

.htaccess error after change server

I have changed my hosting and get a 404 error on one website where we have using .htaccess file and code like this, now what happening sub-cat page showing 404 error http://www.natural-stones-india.com/sandstone/Golden%20Brown%20Black.html
.htaccess code is like this ----------- can anyone help?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^sandstone/(.*).html$ product.php?sandstone=$1
RewriteRule ^slate/(.*).html$ product.php?slate=$1
RewriteRule ^marble/(.*).html$ product.php?marble=$1
RewriteRule ^granite/(.*).html$ product.php?granite=$1
RewriteRule ^artifacts/(.*).html$ product.php?artifacts=$1
RewriteRule ^page/(.*)/sandstone/(.*).html$ product.php?page=$1&sandstone=$2
RewriteRule ^page/(.*)/granite/(.*).html$ product.php?page=$1&granite=$2
RewriteRule ^page/(.*)/marble/(.*).html$ product.php?page=$1&marble=$2
RewriteRule ^page/(.*)/artifacts/(.*).html$ product.php?page=$1&artifacts=$2
RewriteRule ^page/(.*)/slate/(.*).html$ product.php?page=$1&slate=$2
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^action/(.*)/id/(.*)/cat/(.*)/sub_cat/(.*)shoppingcart.html$ shoppingcart.php?action=$1&id=$2&cat=$3&sub_cat=$4
RewriteCond %{HTTP_HOST} ^natural-stones-india.com
RewriteRule ^(.*)$ http://www.natural-stones-india.com/$1 [R=301,L]
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 /missing.html
OK, let's analyze some facts
Webserver does not respond with 500 Internal server error)
Conclusion: Your .htaccess file seems to be syntactically correct
Webserver does some of the rewrites correctly, also the custom error document is working
Conclusion: Your .htaccess file is actually read by the webserver
By the first look all rewrites based in the document root seem to work
http://www.natural-stones-india.com/granite.html works, while http://www.natural-stones-india.com/granite/Pink%20Granite.html does not
Conclusion: You have a problem with relative paths
Possible solution:
RewriteEngine On
RewriteBase /
...
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteBase
Or set the rewrite targets absolute
e.g.
RewriteRule ^sandstone/(.*).html$ /product.php?sandstone=$1
Try changing your access rights to the .htaccess file
You should be able to change these through FTP.
You site seems fine, since this i working: http://www.natural-stones-india.com/product.php?sandstone=Golden%20Brown%20Black

Categories