Forcing HTTPS for a specific url - regex

How, in htaccess, can you force a URL to be HTTPS at the beginning even if it was specified at HTTP?
E.g. the follwoing urls
/subscribe/spanish
/subscribe/english
I'm assumming there is some kind of regex that can be used with /subscribe at the start?

Yes, you can use following rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^subscribe/ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NC]
This will force https if:
URI starts with /subscribe/
URI has http scheme

Related

apache mod_rewrite change base-url in deeplinks

The whole URL of my system is changing, as it is moved to a different location. I'd like to use my old Apache to forward 301 all calls to the new system for some time.
I have deeplinks like this:
https://old.example/groups/something
which will stay the same on the new system - but with a different base-url, so like:
https://new.example/groups/something
I'm looking for an Apache rewrite rule that sustains everything after old.example/ while changing the base-url part and sending the user over to the deeplink location he asked for in the new system.
I tried:
RewriteCond %{HTTP_HOST} old\.example$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* https://new.example/ [R=301,L]
but this only works if the base URL is called directly, not for deeplinks.
RewriteCond %{HTTP_HOST} old\.example$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* https://new.example/ [R=301,L]
Your second condition (RewriteCond directive) is specifically checking that the REQUEST_URI is the document root ("base URL") only. You are also not passing the requested URL-path to the target URL.
However, if the new site has moved to a "different location" and the new and old domains point to different servers then you can use a simple Redirect directive on your "old Apache" server to redirect everything and maintain the same URL structure. For example:
Redirect 301 / https://my.new.system.url.net/
The Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, a request for /groups/something is redirected to https://my.new.system.url.net/groups/something.
Test with a 302 (temporary) redirect to avoid potential caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
Aside: Just a quick note on the "unique" terminology you have used to describe the URL, as it's a bit confusing...
What you call the "base-url" is really the hostname (or domain name).
And what you call a "deeplink" is really the URL-path (or the remainder of the URL). The URL-path starts with the first slash after the hostname.

How to redirect all subdomain urls to main domain

For some reason search engines are indexing my addon domains on my hosting. They should not do that.
For example I just found urls like
addondomain/maindomain.com
how to prevent this happening? How did search engines even find my addondomains?
What is the solution here? I tried this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteRule ^(.*)$ http://www\.maindomain\.com [L]
but when I visit the url for example
addondomain/maindomain.com for example nothing happens?
Try to Change in .httaccess file. You can replace your rule with this rule::
RewriteCond %{HTTP_HOST} =shop.domain.abc
RewriteRule ^ http://www.domain.abc/? [R=301,L]
Using %{REQUEST_URI} will cause original URI to be copied in target. Trailing ? in target will strip off any pre-existing query string.
This answer is a further explanation to my comment on your question.
how to prevent this happening? How did search engines even find my
addondomains?
Google can easily find these subdomains on your site. To prevent this from happening, you can set a redirection with a 301 status code to inform Google that it should not index the addon domain. By doing this, Google will update its index as well.
This is a very common scenario with shared hosting and specially when you use CPanel. In Hostgator's support pages, you can see they have mentioned about this behavior.
Addon URL Example
For the primary domain abc.com, if you assign the addon domain 123.com
to the folder "123," the following URLs would be correct:
abc.com/123
123.abc.com
123.com
All three of these paths would access the same directory and show the
same website. For visitors going to 123.com, there is no evidence that
they are being routed through 123.abc.com.
https://support.hostgator.com/articles/cpanel/what-is-an-addon-domain
You can fix this by adding the following to your .httaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
NC - match in a case-insensitive manner.
R - causes a HTTP redirect to be issued to the browser. When given as
R=301 it will be issued with a 301 status code which is required to
inform Google that their index should be updated accordingly.
L - Causes mod_rewrite to stop processing the rule set. In most
contexts, this means that if the rule matches, no further rules will
be processed.
===========================================================
Edit: Updated to add redirection to all domains, as requested in the comments.
To do this, you can simply check if the hostname is equal to your main domain, and if it's not, redirect it to the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
Hope it helps :)

Amazon Web Services - Redirect www. traffic to non www

I would like to 301 redirect www.socholotiukmma.com to socholotiukmma.com
I've followed this tutorial: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-domain-route-53/ but it's not working.
I'm assuming it's because I'm redirecting to an https site. Is there another way to accomplish this?
You can achieve this using a .htaccess file.
Create a file called .htaccess and put this inside your document root (i.e. the main directory where you put your website files):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.socholotiukmma\.com [NC]
RewriteRule ^(.*)$ http://socholotiukmma.com/$1 [L,R=301]
This should then redirect everybody going to http://www.socholotiukmma.com to http://socholotiukmma.com
If you want to redirect everybody to https://socholotiukmma.com then just change the URL in the final line of the file.

URL Mask/Cloak with redirect

Am trying to do the following. My website is hosted on
www.sitehost.com/uk
But I own this domain.
www.mainsite.co.uk
Is it possible to redirect the user hitting www.mainsite.co.uk to www.sitehost.com/uk but retain the www.mainsite.co.uk?
I tried doing .htaccess redirect and it worked but it changed the URLs from www.mainsite.co.uk to www.sitehost.com/uk
Ideally it would work like so...
www.sitehost.com/uk/post/20
can be accessed via
www.mainsite.co.uk/post/20
I tried mod_proxy but it didn't seem to work all the way. Anyone know how to do this? Is this even possible with Apache?
This is possible if mod_proxy is enabled in your Apache config.
Once mod_proxy and mod_rewrite are enabled place this rule in your DocumentRoot/.htaccess file of sitehost host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?sitehost\.com$ [NC]
RewriteRule ^ http://www.mainsite.co.uk%{REQUEST_URI} [L,P]
P flag is used for proxying the request to external URL.

Exlude a directory in .htaccess with RedirectMatch for Wordpress

I change my Wordpress permalink structure from %year%/%months%/ to %postname%
So i create this mod alias rules in .htaccess but i have a problem:
RedirectMatch 301 /\d{4}/\d{2}/([^/]+)(/?)(.*)$ http://domainname.com/$1
The rules also redirect the images under the wp-content directory, so domainname.com/wp-content/uploads/2013/11/name.jpg become domainname.com/name.jpg
I can't use RewriteCond because this RedirecMatch is mod_alias, i try to use !^/(wp-content.*)$ but doesn't work.
How can i solve? It's better to use RedirectMach or RewriteRule (i haven't changed the server)?
Can you post a better regex?
Thank you!
You should probably stick with using mod_rewrite instead of mod_alias because it'll interfere with wordpress' mod_rewrite rules. Both mod_rewrite and mod_alias affect the same request URI at different points in the URL-file processing pipeline, so you could end up getting redirected and rewritten at the same time.
RewriteCond %{REQUEST_URI} !\.(jpeg|gif|png)$ [NC]
RewriteRule ^\d{4}/\d{2}/([^/]+?)(/?)(.*)$ http://domainname.com/$1 [L,R=301]
Try ^ and $:
RedirectMatch 301 ^/\d{4}/\d{2}/([^/]+)(/?)(.*)$ http://domainname.com/$1