RewriteRule just goes to 301 and not 404 - mod-rewrite

RewriteRule applied only does 301 redirect and not 404 even though 404 is placed before 301.
<If "%{HTTP_HOST} =~ /abc(\.co)?\.in$/">
RewriteEngine on
RewriteRule ^/wp-content/uploads/01/(.*)$ - [L,R=404]
RewriteRule ^/wp-content/uploads/02/(.*)$ - [L,R=404]
RewriteRule ^.*$ https://abc/ [QSD,L,R=301]
</If>
The expected result is to return 404 on all url's with wp-content/uploads/01 and 02 folders.
Currently, all the url's are returning 301 including the wp-content ones.
Not sure what is wrong.
do you think adding a conditional statement would help? but the problem here is there are other rules with many other if statements.

Figured out that it was a problem with RegEx. Needed to escape the forward slash.
^.*\/wp-content\/uploads\/(01|02)\/(.*) - [L,R=404]
https://example.com/wp-content/uploads/01/asd

Related

Apache 2.2 rewrite rule for / is not redirecting

I have config file in apache that should redirect / request to different url.
But i am getting 404 when i hit /
Request is going to backend server but it is not redirecting.
Config file is
#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
RewriteCond %{REQUEST_URI} ^/folder1/folder2
RewriteCond %{REQUEST_URI} !^/(.*)\.(json|html)
RewriteRule ^/(.*)$ http://app2.adobecqms.net/$1.html [P]
RewriteCond %{REQUEST_URI} ^/analytics.txt
RewriteRule ^/(.*)$ http://app2.adobecqms.net/page1/page2/page3/$1 [P]
#I also tried with below. But no luck
#This is returning 502
#RewriteRule ^/$ https://app1.abc.com/page1/page2.html [P]
Any idea?
#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
Because you are using a mod_alias RedirectMatch directive, which is processed after the other mod_rewrite (RewriteRule) directives despite the order of directives in the config file.
And/or you have ProxyPass directives that forward other requests? mod_rewrite is required to override this.
You need to use mod_rewrite RewriteRule here also in order to avoid the conflict. For example:
RewriteRule ^/$ https://app1.abc.com/page1/page2.html [R=302,L]
And this directive should be before the existing directives. (Although there shouldn't be a conflict in this case.) The L flag is required.
The P flag sends the request through mod_proxy - this isn't a "redirect".
The RedirectMatch directive you posted would default to a 302 (temporary) redirect, as I used here. However, if this is intended to be permanent then change to a 301, but only once you have confirmed that it works as intended (to avoid caching issues).

How to redirect httaccess

I tried to redirect but not working even I put the correct code from take it from web.
Redirect /index.php?page=8 /?page=8
Redirect /index.php?page=8 /?page=8
The mod_alias Redirect directive does not match against the query string, so the above directive will never match, so does nothing.
To remove the index.php (directory index) from the visible URL, you would need to use mod_rewrite at the top of your .htaccess file. For example:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ / [R=301,L]
The above will redirect a URL of the form /index.php?page=8 to /?page=8. Any query string present on the initial request is simply passed through to the target/substitution unaltered.
The condition that checks against the REDIRECT_STATUS env var ensures we don't get a redirect loop caused by mod_dir (or the Laravel front-controller) rewriting the request to index.php.
Clear your browser cache and test first with a 302 (temporary) redirect.
However, if you did only want to redirect the specific URL /index.php?page=8 (as stated in the question) to /?page=8 then you should write the rule like the following instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/index\.php?page=8\sHTTP
RewriteRule ^index\.php$ / [R=301,L]
Your htaccess code should be.
RewriteEngine On
RewriteRule ^index.php?page=$1 /?page=$1 [R=301,NC,L]

Basic mod_rewrite difficulties

My goal is to rewrite
http://domain.com/subdir/index.php?p=page-title
as
http://domain.com/subdir/page-title
The code below looks right, but I get a 404 Not Found?
RewriteEngine on
RewriteRule ^/subdir/([^/.]+)/?$ /subdir/index.php?p=$1 [L]
Please, remove the leading slash from your rule (assuming it's in .htaccess instead of httpd.conf)
RewriteEngine on
RewriteRule ^subdir/([^/.]+)/?$ /subdir/index.php?p=$1 [NC,QSA,L]

mod_rewrite "400 Bad Request" problem

I can't seem to get past a Bad Request error while setting up mod_rewrite. I've been trying for a while, so here's what I have.
The url I'm trying to access is:
gordons.local/brewCalc
The page I'd like to see is
gordons.local/index.php?page=brewCalc
Here's my rewrite rule:
RewriteEngine on
RewriteLog /var/www/gordons.com/logs/rewrite.log
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
I've used a regex tool, and this tool, but no matter what I end up with a page that says:
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.12 (Ubuntu) Server at gordons.local Port 80
Also, I'm not getting any information in my access, error or rewrite logs.
EDIT: My rewrite rules are in my vhost file. (/etc/apache2/sites-available/gordons.local)
In case anybody ever finds themselves here, my issue was a missing leading slash before the replacement.
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
Should have been
RewriteRule ([^/]+)/?$ /index.php?page=$1 [L]
Grrrr....
If you see Apache's error.log you would be able to see the actual error. Most likely you are trying to put above rules in .htaccess file and RewriteLog is not allowed in .htaccess file. Also your RewriteRule will redirect more than you intend. So if you comment out your RewriteLog and have your RewriteRule like this then it should work:
RewriteEngine On
RewriteBase /
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# forward to index.php
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA,NC,NE]
NC - Ignore case comparison
NE - Do not encode RHS URI
QSA - Append existing Query String into new one
L - Mark it last rule

mod_rewrite - redirect from one domain to another and preserve trailing values in url

I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

Resources