Htaccess routing returns 404 not found - php

Recently, I tried using htaccess for my semantic cleaner urls. This is my code
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]
When I navigate to http://localhost/home.php?id=1 it returns the right thing but when I go to the http://localhost/products/1 it returns a 404 not found error.
I tried the question: htaccess rewrite returns 404 not found and htaccess problem 404 not found but nothing works

You can use the following :
RewriteEngine on
Options -Multiviews
#rewrite /filename to /filename.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ $1.php [L]
# /product/123 to home.php?id=123
RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]
Your rule returns a 404 when the requested URI is /product/1 because your first rule rewrites it to an unknown location /product/1.php . I added a condition in the first rule so it checks if the request is actually for a .php file.

Related

htaccess rewrite rules are not working

I am trying to redirect my page from /index.php to /index?action=login but my .htaccess file is not working. I checked with
a2enmod rewrite
it returned
Module rewrite already enabled
I wrote a simple expression to redirect from index.php to index. But this is even not working
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index/$ /index.php [R=301,L]
I can't understand the mistake I was doing thanks in advance
To redirect /index.php to /index.php?action=login you can use the following rule :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /index.php?action=login [L,R=301]
The RewriteCondition RewriteCond %{QUERY_STRING} ^$ checks to see if there is no query string in the old uri . Otherwise without the condition you will get a redirect loop error since both the old and the destination uri are identical.
RewriteEngine On
RewriteRule ^([^/]+)/? index.php?action=$1 [L,QSA]
With this rule you redirect from /login to /index.php?action=login

The Page 404 not working with .htaccess?

i have this Code in my .htaccess file:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
ErrorDocument 404 https://example.com/erorrpage.php
ErrorDocument 403 https://example.com/erorrpage.php
i have one argument like this:
https://example.com/var
Your ErrorDocument is not getting triggered because of the following Rule you have in your htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
The Rule redirects everything that doesn't exist as directory !-d and !-f file to /index.php . Since mod-rewrite Rules are applied before mod-core your ErrorDocument will never get applied.
To solve this,You need to use a mod-rewrite Rule instead of ErrorDocument to Redirect non-existent requests to 404 page. So put the following at the top of your htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ - [R=404,L]
If you want to redirect 404 requests to a specific page ,you can use the following
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /errorpage.php [R= 404,L]
I will suppose that you use apache.
Ensure that your .htaccess file are on the right directory (like root directory for example) or erorrpage.php is accessible on the https://example.com/
Try to simplify it with ErrorDocument 404 "Custom very simple 404 error", just to see if it works
Ensure that your mod rewrite is enabled (on httpd.conf, uncomment the ;LoadModule rewrite_module modules/mod_rewrite.so if not. Then restart apache)
Find the relevant directory tag for your www root (on other words, on your apache config of your root directory) and change AllowOverride None to AllowOverride All
If the problem occured in a web hosting, the ensure that it allow the .htaccess
If all of these does not work, try to check apache official docs

clean url with .htaccess but with only one url

i'm using .htaccess to create clean urls.
here is a piece of .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^posts/page/([0-9]+)/$ posts.php?page=$1
RewriteRule ^posts posts.php
it works fine but problem is both urls work!
is there a way to only make site.com/posts/ work and return 'not found' on site.com/posts.php(or redirect to site.com/posts/)
think i read somewhere two urls for the same page is bad for seo.
You can create a rewrite rule that returns 404 HTTP code on every ".php" URL :
RewriteRule .*\.php$ - [R=404]
This is working fine for me. Hope it will work fine for you as well.
RewriteEngine On
Options -Multiviews
#For rewriting request to posts.php
RewriteCond %{REQUEST_URI} ^/posts/?$
RewriteRule .* /posts.php [L,END]
#For returning 404 on directly accessing /posts.php
RewriteCond %{REQUEST_URI} ^/posts.php$
RewriteRule .* - [R=404,L]
To redirect /posts.php to /posts you can use this
RewriteEngine on
RewriteCond %{THE_REQUEST} /posts\.php [NC]
RewriteRule ^ /posts [NE,L,R]
#Your other rules
If you want to redirect the orignal request to 404 , change the RewriteRule line to this
RewriteRule ^ - [R=404,L]

htaccess 404 redirect does not working

I have a .htaccess file to manage rewrite_rules on my website. I also set a 404 redirect command to navigate visitors to a specefied page if they enter a wrong URL. But this redirect does not working and I get 500 Internal Server Error if visitor request an invalid URL. Below is some part of my codes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?lang=$1&page=home [NC,QSA,L]
ErrorDocument 404 http://www.mywebsite.com/404/
What is missing in my code that causes this problem?
The culprit, by seeing all of your .htaccess rules is:
RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
Giving your sample URL http://www.domain.com/en/news/1/something/test/, when a URL does not match with defined rules it hits this last rewrite rule and it falls into an infinite loop:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/(p[0-9]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^(.*)$' to uri 'en/news/1/something/test/'
...
Then we have a recursion start:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
Although a URL like http://www.domain.com/en/news/1/wrong is not found, it's caught by a defined rule (first rule) and we don't encounter any recursion. So this internal error is not thrown on all not found pages but those that are not caught by a rule.
I don't see it right, just remove it.
you should put
<IfModule mod_rewrite.c>
RewriteEngine On
in the start and
</IfModule>
in the end of your .htaccess code
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
You can use an EroorDocument directive to forword non existent requests to a specific destination
Test this in an empty .htaccess file
ErrorDocument 404 /page.php
Check below code, I have write with various scenario.
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
It seems your mentioned URL after ErrorDocument is not valid or is not reachable.
So, what happening is that when it gets a 404 it redirects to your URL, and it gets a 404 again.
Forms a recursive loop.
Getting my point. ?
Try with a simple static html page.
I hope this shall work.
Custom 404 will never work if you add domain like http://www.mywebsite.com/404/ (it's not coding karma), when you type url like this it will ended up in 500 error. It means it executed 404 but redirected user to 500 error due to wrongly configured htaccess.
When error comes in series of 4XX are knows as client side errors and 5XX known as server side errors. For detailed info, 2XX known as Success & 3XX known as Redirection.
So here is quick try (i assume you have 404 html file)
ErrorDocument 404 /404.html
instead of
ErrorDocument 404 http://www.mywebsite.com/404/
I also have tested this code at my side and it worked. Please try out
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
ErrorDocument 404 /404.html
Try this one:
ErrorDocument 404 /404/
Try adding before rewriterules
RewriteEngine On

htaccess two commands in the same file

Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.
Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase

Categories