Apache mod_rewrite links proxy - apache

I want to make a proxy for external links with apache's mod_rewrite module.
I want it to redirect user from, ie http://stackoverflow.com/go/http://example.com/ to http://example.com/ where http://stackoverflow.com/ is my site's URL. So I added a rule to .htaccess file.
RewriteRule ^/go/http://(.+) http://$1 [R=302,L]
But it doesn't work at all. How to fix this?

I am not sure if Apache or the browser reduces // to /, but since it doesn't change the directory one of them reduces this to a single slash on my setup. That's why the second slash has a ? behind it in the rule below:
RewriteRule ^go/http://?(.*)$ http://$1 [R,L]
This will redirect the user to that domain.

This will rewrite all urls (without the beginning http://) to new complete URL. If you're gonna use https links also, you need something like the second rule.
RewriteRule ^go/(.*) http://$1 [R=302,L,QSA,NE]
RewriteRule ^gos/(.*) https://$1 [R=302,L,QSA,NE]
I also added the QSA if your need to include parameters

Related

Exclude a URL from folder redirection

I am redirecting all URLs from www.example.com/forums to www.example.com/blog/.
so I made this rule in .htaccess:
RewriteRule ^forums blog/$1 [L,R=301]
the thing is that I want to exclude some URLs that also begin with forums/ and redirect them to particular URL other than /blog.
For example, forums/8/some-made-up-word-here-1681 to /studies/some-made-up-studies.
Right now, it redirects to /blog like all URLs that start with forum/
You just need to include the more specific redirects first, before the more general rule. For example:
RewriteEngine On
# Specific redirects
RewriteRule ^forums/8/some-made-up-word-here-1681 /studies/some-made-up-studies [R,L]
# Redirect all other URLs that start /forums
RewriteRule ^forums/?(.*) /blog/$1 [R,L]
I've also modified your existing directive to redirect /forums/<something> to /blog/<something>, which I assume was perhaps the original intention, since you were using a $1 backreference in the substitution, but did not have a capturing group in the RewriteRule pattern. Your original directive would have redirected /forums/<something> to /blog/.
I've also included a slash prefix on the substitution. This is required for redirects, although you may have set RewriteBase instead, in which case you do not need to do this.
You will need to clear your browser cache before testing, since the earlier catch-all 301 will have been cached hard by the browser. For this reason it is often easier to test with temporary 302s in order to avoid the caching problem. Change the above temporary redirects to 301s only after you have confirmed this is working as intended.
UPDATE: To redirect all URLs that start /forums to /blog/, without copying the remainder of the URL, then change the last directive to read:
# Redirect all other URLs that start /forums
RewriteRule ^forums /blog/ [R,L]
Basically, the $1 in your original directive was superfluous.

Redirect all pages from old domain to new specific URL without trailing slash content

I've got an old domain, let's say oldomain.com where I need to redirect all traffic to a specific URL, newdomain.com/path
While redirects from oldomain.com go perfectly, anything with content after the trailing slash will be copied over in the newdomain url structure causing 404's.
For example visiting: oldomain.com/somepage will result in newdomain.com/pathsomepage
What I'm looking for are some rewrite rules that will redirect any and all traffic from oldomain.com to newdomain.com/path without changing the specific "newdomain.com/path" URL.
I'm currently using the rules bellow which leads to the result above:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com
RewriteRule ^(.*) https://newdomain.com/path [P]
PS: the redirect is going to a Magento store.
You are trying to reverse-proxy in your directives (the P flag in the rewrite), but since you are describing a redirect... In the old virtualhost you just need to add a simple Redirect directive like this:
Redirect / https://newdomain.example.com/
This will Redirect all requests no matter how they are made to the new domain. Example GET /something will be redirected to https://newdomain.example.com/something
If you want the target to be a fixed destination like https://newdomain.example.com/path no matter what, use RedirectMatch instead:
RedirectMatch ^ https://newdomain.example.com/path

301 redirect but change URL to all lower case in apache

I'd like to set up a 301 redirect in Apache to change the case of the original address before redirecting. So for example if someone enters:
www.website1.com/UsErOfMiXedCase
it should forward to
www.website2.com/userofmixedcase
Would this be a redirect or would it need to be a rewrite? I'm not fussed about individual page forwarding (eg www.website2.com/userofmixedcase/whatever.php) - just www.website1.com/whatever.
Thank you in advance,
Richard
You need to define the rewrite map using Apache's internal tolower function. This can only be done in vhost or server config, and will result in an error if you try to put these directives in an htaccess file:
RewriteEngine On
RewriteMap lowercase int:tolower
Then, in your htaccess file, you can use something like this above any rewrite rules you already have. The redirect rules must be before whatever rules you may have that does routing:
# check that the lower case version of the URI is different than the unchanged URI
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteCond ${lowercase:%1}::%1 !^(.*)::\1$
RewriteRule ^/?(.+)$ http://www.website2.com/${lowercase:$1} [L,R=301]
This will redirect a request like http://www.website1.com/UsErOfMiXedCase to http://www.website2.com/userofmixedcase, thus replacing the URL in the browser's address bar with the one that's all lowercase. Note that it won't affect URLs with multiple path nodes, e.g. http://www.website1.com/SoMe/PathName/UsErOfMiXedCase. If you want it to affect all requests including ones that have multiple paths/subdirectories, then you need to change this line:
RewriteCond %{REQUEST_URI} ^/([^/]+)$
to:
RewriteCond %{REQUEST_URI} ^/(.+)$
You are wanting to use mod-rewrite for this. Something like:
RewriteRule ^/(.*) http://website2.com/$1 [NC]
That is a very general rule, so anything after the leading slash will be lower case. You may want to only do the lower case for specific portions of URL, but I cannot speak to that.
You probably should eyeball the Apache mod-rewrite documentation about the NC flag about this as well.
hth!

Making sure my rewrite rule is a 301 re-direct

I have the following re-write rule which directs krmmalik.com to krmmalik.com/me
How do i make sure this rule is a 301 re-direct, and if it isnt one already, how can i turn it into one?
I've tried using the mixing and matching the tips from this site
http://www.webweaver.nu/html-tips/web-redirection.shtml
as well as Google's Support Articles and existing SO questions, but not having much luck. Note the re-write rule in itself so far has been working fine.
I've also added a CNAME for "www" to "krmmalik.com" in my DNS file. Is that good enough, or do i need to add a specific 301 redirect for that as well?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?krmmalik.com$
RewriteRule ^(/)?$ me [L]
Try the following:
RewriteRule ^/?$ /me [L,R=permanent]
The R=permanent flag instructs a 301 status redirect (and you can use R=301 if you prefer, but I think that "permanent" is more readable).
Putting a forward-slash at the start of the /me target URL will tell Apache to redirect the user to the directory named "me" at the web server's public root directory. So in your case it should redirect the user to krmmalik.com/me (or www.krmmalik.com/me).
Also, you don't need to wrap the match pattern in parentheses, because you don't need to capture the slash for later use. So ^/?$ will do the job fine.

Apache rewrite from root folder, force https

I would like to redirect traffic from mysite.com to mysite.com/folder, but also enforce SSL on all pages.
Currently we have this in httpd.conf we found from a sample somewhere, but I don't think that it is working correctly all of the time.
RewriteEngine on
RewriteRule ^/$ https://www.mysite.com/folder/ [R]
The ^/$ only matches if the request is for exactly /, i.e. the root of the site. Anything else is not matched by it. Try .* instead.
Note: if this rewrite rule is active for both the HTTP and the HTTPS version, that'll send you into an infinite redirect loop. In that case, you may need some kind of RewriteCond test, too.