.htaccess Pages open even though the URL is broken - .htaccess

I have a problem with my website opening pages that should show 404 error. It seems that only the first part of the URL is taken into account.
Here's an example
example.com/page.php - (opens - correct)
example.com/asdasd.php - (error 404 - correct)
example.com/asdasd.php/asdasd - (error 404 - correct)
example.com/page.php/asdasd - (opens - incorrect)
example.com/page/ - (opens - incorrect)
example.com/page/asdasd (opens - incorrect)
I would like to get an Error 404 and if I feel that the URL is important also be able to Redirect it. Now I can't even Redirect URLs that for example look like this "example.com/page.php/asdasd"
The closest answer that I found was this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.php [L]
But the problem was that I got something like this:
Input URL
example.com/page.php/asdasd
Output URL
example.com/page
I need it for the .php extention to stay. And if possible instead of "Redirecting" to the example.com/page.php, I'd like to get an Error 404
This is what I currently have in the htaccess file:
ErrorDocument 404 /404.php
ErrorDocument 401 /404.php
ErrorDocument 403 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ / [R=301,L]
Sorry if this is a duplicate but I don't even know what I should look for.

example.com/page.php/asdasd - (opens - incorrect)
This is due to a feature called Path Info - if Apache manages to match the first part of the requested URL to an existing file, then it will serve that file, and pass the rest of the path along via environment variables (so that a script could in theory make use of this information.)
The AcceptPathInfo directive makes it possible to disable this behavior.
https://httpd.apache.org/docs/2.4/mod/core.html#acceptpathinfo

Related

Magento2 how to remove %2f from URL

When we are hitting the URL domianname/customer/account/login/%2f we are getting plain page instead of 404 Page without Header and footer
we have try with .htacess by adding
RewriteCond %{QUERY_STRING} ^(.*)((%2F)*)
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]
still showing same result
also we have try
Added
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/.*)\.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/error\.php
RewriteRule . - [R=404,L,NS]
ErrorDocument 404 /error.php?e=404
is there anything need to be done at server level ? or it can be handle through .htaccess ?
[1]: https://i.stack.imgur.com/qycvk.pngenter code here

URL rewrite for language query variable to fake directory conversion returns 404 error

I have surely read and tested all the solutions found on Stackoverflow but couldn't make it work ... so here I am.
I'm trying to transform
example.com/?l=en
into
example.com/en/
( /en/ isn't supposed to exist on the server )
I use the following htaccess
DirectoryIndex index.php
ErrorDocument 404 https://example.com/
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{QUERY_STRING} (^|&)l=en($|&)
RewriteRule ^$ /en/?&%{QUERY_STRING}
RewriteCond %{QUERY_STRING} (^|&)l=ja($|&)
RewriteRule ^$ /ja/?&%{QUERY_STRING}
Since the last line triggers a 404 error, the errorDocument 404 kicks in.
The rule returns a "page not found" in the browser when I comment the errorDocument line.

Redirect 404 to search page

Becouse in google webmasters I have 550.000 not found pages - 404 - I want to make some redirections to the search page.
To be userfoul for the users, need to go to the search page, but is realy complicated for me, becouse I need to EXTRACT the search phrase from the 404 url:
Here are the 2 type of url with 404 errors, and need to be redirected.
DOMAIN/movie/12298295-Iron-Man-3
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10
Of corse, I need to extract JUST THE NAME of the movie, or the serie and redirect to this url
DOMAIN/search?q=Iron-Man-3
DOMAIN/search?q=The-Walking-Dead
Well this is the URL by parts:
For "movies" just have 4 parts:
DOMAIN - of corse, is the domain name of the website
/movie/ - is created based on the content type (movie or serie)
12298295- is the ID for this item and can be form - 1 - to
infinite
Iron-Man-3 - the name of the movie (is wath I want to get)
For "series" - THE SAME like for the movies, but at the end can have more parts, and the full URL can be: (all this url, for an normal page with NO 404, are ok)
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/episodes/10 - Link to episode page
DOMAIN/serie/15108-The-Walking-Dead/seasons/3/ - Link to some season page
DOMAIN/serie/15108-The-Walking-Dead/ - Link to the TOP page for this serie
Please note, I need to redirect this url, JUST if the result is an 404 page (ErrorDocument 404), becouse the same type of url are used in all pages.
Here is my actual .htacces
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=301,NC,NE]
RewriteCond %{QUERY_STRING} ^q= [NC]
RewriteRule ^busqueda\b /busquedas [R=301,L,QSA]
# Redirect Trailing Slashes...
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Many thanks for your help
The following shall work for both movies and series:
RewriteRule ^(?:movie|serie)/\d+-([^/]+) /search?q=$1 [R=301,L,NC]

Redirect to subfolder with same name on localhost

I have a webproject saved in a "base" folder of the domain "example.com" which contains a main.php and several subfolders. The second part of the code below redirects all requests (except main.php itself and page404.php) to this main.php and hands over the originally requested URL in the variable "path".
In addition there is a first part which redirects all requests of pages of the baseurl to a subfolder "folder1". So in the end the request of
www.example.com/somepage.php
will lead to
example.com/main.php?path=www.example.com/folder1/somepage.php
(Requests to other subfolders shall only be redirected to the main.php. So www.example.com/somefolder/somepage.php will lead to example.com/main.php?path=www.example.com/somefolder/somepage.php - without adding "folder1".)
The code below actually does what I want:
ErrorDocument 404 /page404.php
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
#first part: redirect to folder1/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/*
RewriteCond %{REQUEST_URI} !^page404*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ folder1/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ index.php
#second part: redirect to main.php
RewriteCond %{REQUEST_FILENAME} (.*)\.php [NC]
RewriteCond %{REQUEST_URI} !main\.php
RewriteCond %{REQUEST_URI} !page404\.php
RewriteRule ^([^?]*)$ /main.php?path=$1 [NC,L,QSA]
</IfModule>
But I have two questions:
Side question: I have the feeling that actually redirecting to "folder1" is way to complicated (even though it works) when I only want to add the "folder1" to the path variable in case a file of the base folder is called. Can you show me a better way to archieve this?
Main question: I have the same project on a localhost where the main folder is named "folder1" which contains the folder "folder1". So there the request of htttp://localhost/folder1/somepage.php shall lead to htttp://localhost/folder1/main.php?path=htttp://localhost/folder1/folder1/somepage.php (I replaced "http" by "htttp" to avoid automatic link recognition of SO) - How do I have to change the code above to have it working in the "localhost/folder1" scenario?
This stuff can be simplified:
ErrorDocument 404 /page404.php
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
# ignore main.php, page404.php OR any files/directory for any rules below
RewriteCond %{REQUEST_URI} ^/(main|page404)\.php
RewriteRule ^ - [L]
#first part: /folder1/path
RewriteRule ^([^/]+?\.php)$ main.php?path=folder1/$1 [L,QSA]
#second part: rewrite to main.php
RewriteRule ^(.+?\.php)$ main.php?path=$1 [L,QSA]

.htaccess - how to use ErrorDocument 404 without 404 in header?

I have some files in /templates/ . Suppose I get an request /templates/something.html. I want to check if the actual file /templates/something.html exists and print it. Otherwise I want my script /search_here.php to do some work and print special output.
I'm using the below in my htaccess file. It works almost fine, the only problem is that in case search_here.php do the work I see a good output, but server sends 404 header (HTTP/1.1 404 Not Found). How can I turn off this header (my /search_here.php will print this header in case of need)?
Any help would be appreciated.
RewriteEngine on
Options +FollowSymlinks
#RewriteBase /
RewriteCond %{REQUEST_URI} !/templates/
RewriteRule ^.*$ index.php [L]
ErrorDocument 404 /search_here.php
You should not use /search_here.php as 404 handler.
You can use:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule !^templates/ index.php [L,NC]
RewriteRule ^ search_here.php [L]

Resources