.htaccess url rewrite but nothing happens - apache

Im currently trying to rewrite the urls of my new site by writing im my .htaccess. ive done redirects for 3 different scenarios and a generel.
The urls doesnt get redirect for nice url.
rewriting this url "http://domain.dk/?fag=1&id=87" to: "http://domain.dk/fag/1/id/87"
rewriting this url "http://domain.dk/?fag=1" to: "http://domain.dk/fag/1/"
rewriting this url "http://domain.dk/?id=87" to: "http://domain.dk/id/87"
my code is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.dk$ [NC]
ErrorDocument 400 /?fejl=400
ErrorDocument 401 /?fejl=401
ErrorDocument 403 /?fejl=403
ErrorDocument 404 /?fejl=404
ErrorDocument 500 /?fejl=500
RewriteRule fag/(.*)/aflevering/(.*)/ index.php?fag=$1&id=$2
RewriteRule fag/(.*)/ index.php?fag=$1
RewriteRule aflevering/(.*)/ index.php?id=$2

You need the AllowOverride directive set to All for the container

Related

.htaccess rewrite to show only root url and hide everything after /folder

From this URL
www.example.com/error_documents/404
to this URL
www.example.com/404
I've tried many different .htaccess rules but none of them worked.
I'm trying to just hide the /error_pages/ folder section from the URL without any actual redirecting because if I write a correct *RewriteRule, its just keep repeating itself and I get an ERR_TOO_MANY_REDIRECTS error, because if you want to go to an unknown folder, the error document redirects to example.com/error_documents/404 and if I rewrite this to example.com/404, its an unknown folder so it is trying to redirect me to the /error_documents/404 page but the htaccess file keeps redirecting to a forever loop.
Current .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+error_pages/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^error_pages/)^(.*)$ /error_pages/$1 [L,NC]
This .htaccess gives me forever loop:
www.example.com/unknownfolder
to
www.example.com/error_documents/404
to
www.example.com/404
and this keeps repeating...
I'm using cPanel for ErrorDocuments and the main .htaccess file is:
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
You shouldn't be using an absolute URL in the ErrorDocument directive in the first place - this is what is causing the external (302) redirect and exposing the location of /error_pages and the error document. Consequently, this also loses information about the request that caused the error.
However, /400 and /401 etc. should reference the actual file(s) that handle the request. eg. /400.html and /401.html etc.
You should be using a root-relative file-path (starting with a slash) to the error document and then Apache will issue an internal subrequest, rather than a redirect.
For example:
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 503 /error_pages/503.html
Your /error_pages now remains totally hidden from the end user.
No need to manually try and remove this from the URL (because it should never be present in the URL to begin with). You can (optionally) prevent direct access to the /error_pages directory if you want (careful not to block subrequests for the error documents).
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

Prevent error 403 on invalid URL parameter

I've links to my website which are clicked by users and not directly in my control to be changed or encoded properly.
URL looks like this
http://mywebsite.com?email=%%username#company.com
I want this URL not to be forbidden and prevent the error 403
On other questions, people have said this "It is mod_security's 10_asl_rules.conf causing this error." I searched it and could not find mod_security on the PHP info page
here is how my .htaccess file looks like
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (.+)=[%25][%25](.+)
RewriteRule (.*) $1/?%1=%2 [R=301,L,NC]
ErrorDocument 403 "We are working on it..."
</IfModule>

how to redirect anyone, accessing a specific folder to receive 404 error with htaccess?

I am trying to redirect people, that try to access a specific folder on web to receive 404
Example: if anyone wants to access www.mysite/myfolder/ I wish that person to receive 404 error
What I have tried by now:
RewriteCond %{REQUEST_URI} ^/node/$
RewriteRule .* - [F]
RedirectMatch 301 www.yoursite/node/
RewriteEngine on
RewriteRule ^//node/123 /node/999 /node/128173/(.*)$ /pages/errors/404.php$1 [R=301,L]
None of those really worked-received 500 error
Also tried to simply redirect everything to a non-existent site, like this:
Options +FollowSymLinks
RewriteEngine On
Redirect 301 /node/*.* http://www.your imaginary site/to-new-url
Yet, still received 500
Can anyone please help?
Why not deny access to /node/ folder?
<Directory /node/>
Deny from all
</Directory>
or modify your rewrites in .htaccess to:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^node/ [NC]
RewriteRule (.*) /pages/errors/404.php [R=301,L]
But it's not good practice, 404 page should return HTTP 404 status not HTTP 301 as redirect.
Hi all and thank you for answering
my 500 error was apparently due to not specifying https in the redirect form
After that everything worked

403 error page not working with F flag in .htaccess

I'm trying to only allow https connections to a subdomain of mine. My .htaccess looks like this currently:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
#Only allow https requests
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/docs/?(.*)$ [NC]
RewriteRule ^ - [F]
#Docs file rewrite
RewriteRule ^docs/?$ /Docs.php
If I remove the Only allow https requests section and try to access a directory (e.g. http://foo.bar.com/images/), I am displayed my pretty 403 page. But if I leave that section in and go to the subdomain root (e.g. http://foo.bar.com/), I get the default error page, is this something to do with htaccess completely forbidding access to all files?
Examples:
Through a secure connection the 403 page works - https://api.subjectplanner.co.uk/assets
But through a non secure one, it is the default page - http://api.subjectplanner.co.uk/assets
RewriteEngine On should be before you use rewrite rules:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
#Only allow https requests
RewriteCond %{HTTPS} off
RewriteRule !^docs - [NC,F]
RewriteRule ^docs/?$ /Docs.php [L,NC]

how to redirect all 302's to a different url as 404's using htaccess or apache?

i am wondering how to redirect all 302's to a different url as 404's using htaccess or apache?
i prefer the htaccess method
maybe something like Redirect 302 /PATH_TO_REDIRECT NEW_LOCATION_URI
but how do i tell the new url to be a 404
any ideas?
you do:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.this-is-ma-new-404.com/$1 [R=301,L]
in your .htaccess.
==
for your own custom 404 use:
ErrorDocument 404 /custom404.html