htaccess rewrite only working without "www" and "https" - apache

I have this case where the rewrite only works without www and https, this is .htaccess:
RewriteRule ^en/(.*)/1348-duracell-32-aaa-pack.html$ /$1/3016-duracell-32-aaa-pack.html [R=301,NC,L]
this URL works and redirects:
http://example.com/en/car-accessories/1348-duracell-32-aaa-pack.html
this one doesn't:
https://www.example.com/en/car-accessories/1348-duracell-32-aaa-pack.html
any ideas? I spent a good few hours on this without any solution, not sure what's wrong. The rule is on top of the .htaccess file.
Any help would be greatly appreciated.

What do you mean by doesn't work ? Is the 2nd URL even redirecting ? If you are on a Linux platform you might want to try curl -L --head <url>, then you can see if redirections or taking place (or a redirection loop). Actually curl is a better way to test your rules, because your browser cache could be playing tricks on you.
Note that $1 is the first captured group from your regular expression. So you should add the host you want to redirect to ie:
RewriteRule ^en/(.*)/1348-duracell-32-aaa-pack.html$ https://example.com/$1/3016-duracell-32-aaa-pack.html [R=301,NC,L]
Otherwise the browser is very likely going to keep using the current host name. If you don't provide a full URL, then it has to make up for the missing bits.
But if all you want is remove /en/ from the URL, then you could write a simpler and more straightforward rule.

Related

How to create a redirect rule for 404 pages with many different path

Here my problem, the site I'm working on has many 404 pages, but they used to be the same pages with a different path
/mens/designers/mens/wales-bonner
/mens/designers/casual-shirts/wales-bonner
/mens/designers/coats-and-jackets/wales-bonner
etc.
THe client wants the redirect to go to the category, so
/mens/designers/mens/
/mens/designers/casual-shirts/
/mens/designers/coats-and-jackets/
I'm pretty sure, there must be a way to have regex rule to cover them all, but I can't seem to find how
Something like
RewriteRule ^/mens/designers/(.*)/wales-bonner /mens/designers/(.*)
but it doesn't work, I don't know how to group the middle part of the URL
Can anyone help ?
I see several potential problems with your rewrite rule:
You have a capturing group in the output URL rather than using $1 to use the result of the capturing group from the input URL.
^/ won't work in .htaccess, only in Apache .conf files. I' would use ^/? instead which will work in either context. That makes the starting slash optional.
You don't include an [R] flag on the rule to make it a redirect.
You don't include an [L] flag on the rule to prevent any following rules from interfering with it.
You can also add "mens/designers" to the capturing group so that you don't have to repeat it in the output. I would suggest:
RewriteRule ^/?(mens/designers/.*)/wales-bonner /$1 [R=301,L]

RewriteRule from mod_rewrite only works as redirect with added http://%{HTTP_HOST}, but doesn't work at all without it

I use this apache config file on centOs 7. This file is is already set up to run
/opt/otrs/bin/cgi-bin/customer.pl
on my machine on a call to
http://localhost/opt/customer.pl
I wanted to use mod_rewrite to run the customer.pl and give it the called url as a parameter.
So i want to enter the url:
localhost/HTMLData/1/Default.htm
and have it call:
/opt/otrs/bin/cgi-bin/customer.pl
with the parameters
?Action=CustomerHTML;Subaction=ContentCall;Target=1/Default.htm
That works somewhat with:
RewriteEngine on
RewriteRule ^/HTMLData/(.*)$ http://%{HTTP_HOST}/otrs/customer.pl?Action=CustomerHTML;Subaction=ContentCall;Target=$1
But i this is a redirect (because of the added "http://%{HTTP_HOST}").
What i need is, that the webrowser keeps thinking http://localhost/HTMLData/1/ is where to get the files.
But if i remove the "http://%{HTTP_HOST}", it doesn't work at all. But the examples i found on the internet said i should work this way. What do i have to change, to have the redirect only internaly without giving the browser the real url back?
Thank you
In case someone else might need this in the future.
What fixed it for me was adding
RewriteEngine on
RewriteRule ^(.*)$ $1
under "". I don't know why, but at least i can use the internal redirect now.

Apache %{REQUEST_URI} not working correctly

I am not using Virtual Hosts or anything fancy though I have some .htaccess files setup. Following is my rewrite rule in httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/app/smsapi [NC]
RewriteRule (.*) https://www.example.com/uri=%{REQUEST_URI} [R,L]
This rule basically says that if the uri does not begin with /app/smsapi then fire the rewrite. But when I restart the server and try it I get some weird results.
When I request the URL https://www.example.com/app/smsapi/index.php, I get a 200 Success code which is as expected. But, when I request the URL http://www.example.com/app/smsapi/index.php, it redirects to https://www.example.com/uri=/app/smsapi/index.php. So it actually fires the rule even though the request URI does not satisfy the condition.
So, then I decided to turn off the rewrite rules and give it a go. Now, both those URL give me a 200 Success code.
Now, I know this problem cannot be solved easily by other people who do not have access to the server, but am I right in saying that this is certainly a problem with REQUEST_URI not firing correctly? I have shown that without the rewrite rule, everything works normally, but with the rewrite rule, the second URL is redirected. Therefore, the redirection must be caused by the rewrite rule? Additionally, the condition for redirect rule is not satisfied. Doesn't this prove that there is something wrong with the functioning of the rewrite rule?
Is there any other possibility?
UPDATE
Something very weird is happening here. I setup a local server and tried the same rule and what I got for the URL http://192.168.0.112/app/ is
http://192.168.0.112/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/uri=/app/
which is correct because as long as the URL is not like /app/smsapi, it should redirect it. Wonder why this is not happening on the real server. Also, where you insert these rules seems to make a difference. (I am only including these rules after the LoadModule command).
On localhost, if I put these rules either above or below the Directory section, it won't work. But, if I include it inside the Directory section it will.
On server, if I include the rules inside the Directory section, they won't work. But, if I include them either above or below the Directory section, they start working.
This seems to me to be due to a difference in the versions. My localhost is an Ubuntu Desktop 16.04 running Apache 2.4.18. While the server is CentOS 6.8 running Apache 2.2.15.
But, i think the mystery as to why on the server redirect happens only once (though it is configured to go upto 20 times) has something to do with https. Which is also related to the original problem in which https is redirected even on a non-matching rule.
Clues anyone?
UPDATE
I updated the httpd.conf file with the same rules but I used http:// instead of https:// and it gave me the correct result with 20 redirects. That means I have isolated the problem to https.
You are reporting the exact issue in the first phrase: "I am not using Virtual Hosts or anything fancy though I have some .htaccess files setup"
.htaccess is "fancy" and overcomplicated, not virtualhosts.
If you had defined that RewriteCond in virtualhost in the first place it would work, but .htaccess is per-dir context (aka a nightmare) and the regex ^/ will never match in that context.
If you want to match REQUEST_URI in per-dir context (directory or .htaccess) you need to drop the initial slash, that is:
RewriteCond %{REQUEST_URI} !^app/smsapi [NC]
Extra, also consider you MAY NOT need to add a RewriteCond for this:
RewriteRule ^(?!app/smsapi)(.*) https://www.example.com/uri=$1 [R,L]

Apache RedirectMatch

I'm working on an apache server (2.2), and I'm trying to redirect a URL based off of a URL filter. For example,
https://mywebsite.com/path/to/page?folder=folderDirectory/folderName
will redirect to:
https://mywebsite.com/static/contentUnavailable.html
In my httpd.conf file I have the following code ..
RedirectMatch (.*)path/to/page?folder=folderDirectory/folderName /static/contentUnavailable.html
I restart apache everytime I make modifications to this file, however the page is not redirecting. What am I doing wrong in the RedirectMatch?
You can't match query string with a redirectmatch, sorry, you need mod_rewrite for this and using a RewriteCond. Rough example:
RewriteCond %{QUERY_STRING} ^folder
RewriteRule ^ /static/contentUnavailable.html [R,L,QSD]
This will match a query string that starts with folder (and continues with whatever else, no matter what it is). and redirect everything to the destination you want, discarding the query string in the process (QSD flag).
In any case let me commend you for trying to stick to redirect/redirectmatch first (while everyone else just goes blindly for mod_rewrite even for the simplest redirects). You are doing things right.

ZendFrameworks not working nice with RewriteRule

I have the following RewriteRule on my Apache with ZendFramework in the back:
RewriteRule ^/account([0-9]+) /account?i=$1 [L]
Problem that I'm having is that I have an AccountController.php for ZendFramework, so URL from browser going to ...://myserver/account works just fine. However, when I have this rewriterule in httpd.conf, and point my browser to ...://myserver/account1, then ZendFrameworks complains that /account1 controller is not found, even though I have the rewriterule in apache. My guess is that ZF is looking at the REQUEST_URI which still have /account1 in it.
Anyone knows now I can make this rewriterule work properly with ZF? What I need is basically to have URLs going to /account([0-9]+) forward to /account?i=$1 instead using my ZF's AccountController.php?
Note I do not want to 'expose' the 'i' param to users (for various reasons, and yes, they could guess at looking at account1 that it can be sequenced), hence it is an internal forward instead of an external redirect back to the browser.
Thanks!
Maybe your rule is overwritten by other Zend rules? By default, Zend takes URLs in the form controller/key1/value1/key2/value2/..., maybe you could just try this?