.htaccess error after change server - php

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

Related

htaccess - redirect to error pages issue (with slash or without slash)

There is an issue with redirection to error pages:
example.com/test - will redirect to 404 error page
but
example.com/test/ - will go to the white "File not found." page
to mention:
it was working properly until some time ago (maybe update of PHP version ??)
same behavior with www/http/https version of the links
standard structure of the links is www.example.com/test/
.htaccess file code
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 410 /410.php
The difference with your URLs that end in a trailing slash is that they are unconditionally rewritten to the corresponding .php file. The URLs that do not end in a trailing slash are not rewritten - nothing happens.
You are seeing the same basic "File not found" response when you directly request a non-existent .php file, regardless of whether the request is rewritten (by your rules) or not.
The "problem" might be due to the way PHP is implemented on your server. For instance, if all *.php requests are proxied to an alternative backend process then this is going to bypass your .htaccess file on the application server and the "basic" 404 response you are seeing is possibly coming from the proxy, not your application server.
You may be able to resolve this by first checking that the .php exists before rewriting to it (so it doesn't trigger a 404). And if none of your URLs contain a .php extension, you could also force any direct request for .php files to 404 (on your server, before the request is proxied - if that is what's happening).
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule sample/(.*)/(.*)/$ /sample.php?$1=$2
The first two rules can also be combined into one. You are missing L flags on all your rules. You need to ensure that MultiViews is disabled, otherwise the last rule will not work.
Also, the regex in the last rule needs to be anchored and made more specific since it is matching too much, eg. /sample/foo/bar/baz/qux will be rewritten to /sample.php?foo/bar/baz=qux, which I assume is not the intention.
Try the following instead:
Options -MultiViews
RewriteEngine On
# Force any direct request for ".php" files to 404
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
# Rewrite to ".php" file - 1 or 2 path segments
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+(/[^/]+)?)/$ $1.php [L]
# Rewrite "/sample/one/two/"
RewriteRule ^sample/([^/]+)/([^/]+)/$ sample.php?$1=$2 [L]
Reference:
Another recent question that has a very similar issue and was resolved in the same way:
Custom 404 error handler in htaccess not working for non-existent ".php" files
The problem is with ending slash of RewriteRule ^([^/]+)/$ $1.php
If you write RewriteRule ^([^/]+)/?$ $1.php the tailing slash would be optional.
edit
You should also add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
before RewriteRule statements, because of server loop - when the file exists the statements will break loop by skipping rewriting.

Htaccess RewriteRule redirects to 404 page

My basic requirement is:
Remove .php extensions from all the urls.
Rewrite the urls from http://localhost/unsync/softwares/page_name/sub_category/ to http://localhost/unsync/softwares.php?p=page_name&sub_cat=sub_category
The following is my .htaccess code:
# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes
#Error Documents
ErrorDocument 400 /unsync/error.php?code=400
ErrorDocument 401 /unsync/error.php?code=401
ErrorDocument 402 /unsync/error.php?code=402
ErrorDocument 403 /unsync/error.php?code=403
ErrorDocument 404 /unsync/error.php?code=404
ErrorDocument 500 /unsync/error.php?code=500
ErrorDocument 503 /unsync/error.php?code=503
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
RewriteRule softwares/(.*)/(.*)/$ /softwares.php?p=$1&sub_cat=$2 [L]
DirectoryIndex index.php
The problem I am facing is that, RewriteRule fails. I mean, when I try to access softwares/page_name/sub_category, its throwing a 404 error.
Note: Its removing the .php extensions properly and working fine with normal pages.
The problem is that your rewrite rule rewrites all requests to /unsync/request.php, you have to check the existance of php file before rewriting the request,
#Remove extensions
RewriteCond %{DOCUMENT_ROOT}/unsync/$1.php -f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
Or you can simply exclude the slash in pattern so that it can not conflict with your other rule
RewriteRule ^([^\/.]+)$ /unsync/$1.php [NC,L]
Rewrite rules are tried in order.
This means softwares/page_name/sub_category is rewritten by the first rule to /unsync/softwares/page_name/sub_category.php, which is not found. Therefore, you get a 404 Not found error.
After a long day of research, I could finally resolve my issue on my own as follow (if anyone facing similar issue is searching for solution):
#If both p and sub_cat are issued
RewriteRule ^softwares/(.+)/(.*)$ /unsync/softwares.php?p=$1&sub_cat=$2 [NC,L]
#If only p is issued
RewriteRule ^softwares/(.*)$ /unsync/softwares.php?p=$1 [NC,L]
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
DirectoryIndex index.php

Weird behaviour of .htaccess index blocking

I had some problems with excluding a folder from the index block in the .htaccess file in my site's root folder.
After messing around a bit, I found a solution:
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
I wrote this in an new .htaccess file in the folder that I wanted to be excluded. This solved my problem (unblocking that specific folder while keeping the subfolders blocked) which is great and all but as far as I know, this piece of code shouldn't do anything. How could this possibly work?
Additional information:
- the excluded folder contains a Nibbleblog installation
- root .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://url.url/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.nyxcode\.xyz [NC]
RewriteRule (.*) https://url.url/$1 [R=301,L]
ErrorDocument 404 /errors/notfound.html
ErrorDocument 403 /errors/forbid.html
Options -Indexes
By adding RewriteEngine on to a .htaccess file in a subfolder, you've told Apache that the parent folder's .htaccess doesn't control rewriting in the subfolder. This is because Apache checks the folder that matches the URL first, and only then looks in the parent folder.
If you have this:
/
.htaccess
/subfolder1
.htaccess
/subfolder2
# no .htaccess file here
Then files in / and /subfolder2 are subject to the RewriteRules in /.htaccess. But /subfolder1 is subject only to its own .htaccess file, to the extent that it applies. Without RewriteEngine on, you haven't told Apache that the /subfolder1/.htaccess applies for rewriting purposes. With RewriteEngine on, you're saying, "Use this .htaccess for rewriting," even if there are no rules there.

URL Rewrite not working on directory

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.

routing with htaccess and laravel

Hello I am using the following htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
For doing the laravel routing.
When I surf to example localdevurl/public/users I get the following error ( 404 )
Not Found
The requested URL /Users/username/Sites/sitefolder/public/index.php/user was not found on this server.
But as u see it shows the index.php in the error. When I put index.php in my url it does work indeed. I have turned on everything in my apache config.
httpd.conf
Can anyone see what I am doing wrong here?
In your .htaccess file, you are passing the request after the index.php part, instead of allowing laravel routing system to process it, this is:
RewriteRule ^(.*)$ index.php/$1 [L]
should be as the original .htaccess
RewriteRule ^ index.php [L]
I had the same problem with the index.php part, this is how I solved: There is a problem when you have userdir module enabled, which it seems to be the case based on the URL of the error message.
Possible solutions:
Create a symlink in the webserver folder to the public laravel project folder of the user web directory. e.g.: # ln -s ~/public_html/mylaravel4/site/public/ /var/www/mycoolsite
Then you can access http://localhost/mycoolsite
Replace RewriteCond and RewriteRule lines with this: FallbackResource /index.php
Make sure you have AllowOverride set to All instead of None for you particular vhost, and also set Order allow, deny rather than Order deny, allow in httpd.conf.
AllowOverride all
Order allow, deny

Categories