htaccess: 404 RewriteRule not working for specific URL - .htaccess

This should be a simple task: Send a 404 redirect for a specific URL: /folder1/folder2
Here's what I've tried:
RewriteEngine on
RewriteBase /
#RewriteRule ^folder1/folder2/(.*)$ /folder1/folder2/$1 [R=301,L] (works, but I want a 404)
RewriteRule ^/?folder1/folder2 - [R=404] (does not work)
RewriteRule ^folder1/folder2 - [R=404,L] (does not work)
RewriteCond %{REQUEST_URI} ^/folder1/folder2
RewriteRule ^ - [L,R=404] (does not work)
RedirectMatch 404 folder1/folder2(.*)$ (does not work)
RewriteRule ^folder1/folder2(.*)$ /not-found.html [L,R=404] (does not work)
# This works, but is not what I need:
RewriteRule ^(.*)$ /not-found.html [L,R=404]
When testing the rules (http://htaccess.mwl.be/) they work as expected, but not on my localhost. What is going on?
I'm running Apache 2.4.
Thanks for your help!

Related

.htaccess Pages open even though the URL is broken

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

.htaccess of symfony make a 302 redirection

Using symfony for my website and i have a 302 redirection from my https://www.exemple.com/ to https://www.exemple.com/, and google doesn't want 302 redirection... It is the last condition of the rewrite section of the .htaccess which does that.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
I tried to put instead :
RewriteRule .? %{ENV:BASE}/app.php [R=301,L]
But in that case i have a 301 redirection but my site is not reachable. Any ideas ?
Find solution,
It was not the .htaccess in cause. I'm new with 301/302 redirection and didn't know u can spécifiate it in controller. So i add a ",301" at the end of redirections i do in controller like that :
return $this->redirect($this->generateUrl('sdzbikind_acceuil'), 301);
Hope it helps somebody else... I'm just asking now if i should do 301 redirection in all my symfony project redirections !?

Url rewrite and trim url

I need your precious help.
I have an url:
http://domain.com/v/12345
My ht access is set to pass 12345 to php script like:
htaccess:
RewriteRule ^v/([a-zA-Z0-9_-]+)$ /script.php?v=$1 [L]
script php:
http://domain.com/script.php?code=12345
and works perfectly but.. and there is a but, I would write my url so:
http://domain.com/12345/my-dog-is-very-fat
can you help to change my htaccess to works so? Because if I use /my-dog-is-fat or just / I get an 404 error.
I try to explain better:
I would share link with seo keywords in the url, example
mydomain.com/alphanumericCode/thi-is-my-dog-article
where alphanumericCode is get of myscript.php?V=
This is my htaccess:
RewriteEngine On
RewriteRule ^view/([a-zA-Z0-9_-]+)$ /view.php?v=$1
RewriteRule ^folder/([a-zA-Z0-9_-]+)$ /filefolderlist.php?f=$1
RewriteRule ^home$ /index.php [L]
RewriteRule ^report$ /report.php [L]
RewriteRule ^play/([a-zA-Z0-9_-]+)$ /play.php?v=$1 [L]
###Added by anubhava
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]
##end
RewriteRule ^privacy-policy$ /privacy.php [L]
ErrorDocument 404 404.php
You can have a new rule for the new URI scheme:
RewriteEngine On
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]

htaccess shortening url

i would like to shorten
www.site.com/product/info/laptop to www.site.com/laptop
I used
RewriteRule ^(.+)$ /product/info/$1
but i get 500 Internal Server Error
when i try,
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1
it works but i want to support the period as well, so when I include .
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1
It gives me 500 Internal Server Error
Could you explain what is going on?
Thank you
Try the following:
RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$ /%2 [R=301,L]
That will redirect the browser from www.example.com/product/info/laptop to www.example.com/laptop with a "Moved Permanently" header.
If you mean you wish the shorter URL to point to the longer URL internally, then you must avoid circular redirections:
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
The first line will stop trying to redirect www.example.com/product/info/laptop to www.example.com/product/info/product/info/laptop, and so-on.
Edit
Based on your comment, it looks like you're also trying to redirect everything except img, phpmyadmin, etc, to index?whatever - You have to rearrange it all a bit now, something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^(.*)$ /product/info/$1 # redirect everything else
I'm not 100% on the "product/info/(.*)" part of the first rewrite. If that doesn't work, try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
Edit 2
Final answer based on your comment:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]
# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else

RewriteRule - a.php?a=123 to /b/123

I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]

Resources