htaccess subdomain redirect and worpdress - wordpress

After setting up Wordpress to use permalinks (which added some lines to my htaccess file), I got confused how to redirect a subdomain to a subfolder (astro.aspiracoesquimicas.net should redirect to aspiracoesquimicas.net/astro): what I had tried (and worked) before doesn't work anymore:
RewriteCond %{HTTP_HOST} ^astro\.aspiracoesquimicas\.net$
RewriteCond %{REQUEST_URI} !^/astro/
RewriteRule (.*) /astro/$1
Now I get a 500 Internal Server Error with the subdomain. I don't know about htaccess configuring and don't understand what changed because of Wordpress.
I'd also like to redirect blog.aspiracoesquimicas.net to the main domain aspiracoesquimicas.net
How can I do it?

Make sure you add the L flag to the rule and make sure you place the rules BEFORE the wordpress rules:
RewriteCond %{HTTP_HOST} ^astro\.aspiracoesquimicas\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/astro/
RewriteRule (.*) /astro/$1 [L]

RewriteEngine On .
RewriteCond %{HTTP_HOST} !^astro\.aspiracoesquimicas\.net/
ReWriteRule ^(.*)$ https://aspiracoesquimicas.net/astro/$1 [R=301,L]
use this method

Related

Redirection with HTACCESS including GET parameters

I have a specific .htaccess defined as is, in order to reroute all traffic from root folder to identical URL in a subfolder with exceptions on sub-domains.
Here's my file:
#RewriteEngine on
#RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !^joe(.*)
RewriteCond %{REQUEST_URI} !^lab(.*)
RedirectMatch ^/$ http://www.mydomain.fr/blog/
</IfModule>
This file does exactly what I want: when I input something like www.mydomain.fr/wp-admin for example, it will redirect to www.mydomain.fr/blog/wp-admin, but will exclude subdomains joe.mydomain.fr and lab.mydomain.fr from redirection.
However, what happens is that if I go to joe.mydomain.fr?test=1, the server will simply redirect me to joe.mydomain.fr but the GET parameter disappears.
How can I avoid that?
I tried replacing [NC] with [QSA] or [R=301,QSA], but I get an Internal Server Error.
Thanks.
After spending my day documenting about how .htaccess files actually work, I was finally able to solve my problem with the following file, inspired by the one from Abhishek Gurjar:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /s
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^joe\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^lab\.mydomain\.fr$ [NC]
RewriteCond %{HTTP_HOST} !^blog\.mydomain\.fr$ [NC]
RewriteCond %{REQUEST_URI} !/joe
RewriteCond %{REQUEST_URI} !/lab
RewriteRule ^(.*)$ http://www.mydomain.fr/blog/$1 [R=301]
</IfModule>
FYI, the 3 first lines are mandatory with my provider 1&1 apparently...
What I changed is the REQUEST_URI from being !^joe(.* ) and !^lab(.*) to !/joe and !/lab and then excluding the blog.mydomain.fr URL as well to avoid problems with blog.mydomain.fr/joe or blog.mydomain.fr/lab if ever one day I use them.
Finally, I had to redirect my sub-domains in my 1&1 client interface to the subfolders of my webspace instead of the URL in hard, meaning:
Instead of redirecting joe.mydomain.fr to http://www.mydomain.fr/joe, I redirect it to /www/joe/
That configuration now allows me to type in an URL such as joe.mydomain.fr?dnt=1 while getting redirected to http://www.mydomain.fr/joe?dnt=1 which is exactly what I wanted to do.
I hope this helps others.
Thanks for the help.

Within .htaccess File https is Off

I am wanting to redirect all my pages to https. Currently it is doing this but some websites when doing test state that I have to many redirects. This is basically what I have set up within my .htaccess file (for Wordpress)"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
NOTE: I also have another redirect within this file for another reason, as I have changed my URLs and wanting them all to redirect using a 301:
RewriteBase /
RedirectMatch 301 /market/(.*)/ /product/$1
I'm basically wondering if I have done it all correctly or is there a simpler way so there is not so much redirects. I also notice within my Plesk panel there is an option for this. Any help would be great.
I suggest using thos code in your htaccess file to force HTTPS redirection (make sure to adjust URLs to your actual ones):
RewriteCond %{HTTP_HOST} !^domain\.local$ [NC]
RewriteCond %{HTTP_HOST} !^staging\.domain\.com$ [NC]
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

Redirect http to https with www

I am trying to find out how to redirect everything (http/ with and without and http: without) to https://www. (with www.)
I know it needs to be written in the .htacess file and I have forces admin to use https in wp-config already. I'm after the exact script to add at the top of my .htacess file please.
I am running on Cpanel. I'm using wordpress and maxcdn with w3cache. maxcdn is already set up for https.
I have found posts on how to write the code but lack the knowledge to create it myself.
Any help is greatly appreciated!
Thanks for your time.
Like Shiv said, .htaccess is definitely the way to go. Here's the complete solution which will redirect to SSL and add www if not present:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]
I think this might do the trick:
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ https://www.domain.com/? [R=301,NE,NC,L]
# 301 Redirect 2
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ https://www.domain.com/? [R=301,NE,NC,L]
Obviously change domain.com to your domain name.

.htaccess rediret from 3th level domains to WordPress page and keep the url

I have a bunch of 3th level domains and I need to redirect them to different wordpress page.
I did it with this code in the .htaccess
RewriteCond %{HTTP_HOST} third\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/subs-page/ [R=301,L]
and it works, but I need to keep the original url in the address bar. Is that possible?
If these subdomains are folders in the root, you can try this.
RewriteCond %{HTTP_HOST} third\.domain\.com$
RewriteRule ^(.*)$ /subs-page/$1 [L]
Or you can use a Proxy pass using the P flag to keep the URL in the browser. Make sure mod_proxy is enabled.
RewriteCond %{HTTP_HOST} third\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/subs-page/$1 [P]

How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www?

This is what I want -
When somebody enters site.com/page.php or site.com/page or site.com/page/ , all should work the same, and the url displayed in the address bar should be site.com/page [ I don't like extensions and trailing slashes ]
I also want to get rid of the www from the url bar, as it's unnecessary.
This is my current .htaccess files Rewrite rules -
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=permanent,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This solves the www and the extension issue, but not the trailing slash one!
But, more importantly, I recently installed WordPress at site.com/blog, but whenever I try to access that, I am redirected to the 404 page instead. It works when I remove all the Rewrites!
Have I messed up my rewrites? I am not a regular expressions ninja!
Try adding the following to the top of your existing .htaccess file, above any existing rules e.g. Wordpress rules.
RewriteEngine on
RewriteBase /
#if the host is not site.com, e.g www.site.com
RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
#then redirect to site.com e.g to remove the www
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]
#if the URL ends with a slash
RewriteCond %{REQUEST_URI} ^/(.+)/$
#redirect to URL without slash
RewriteRule . http://site.com/%1 [R=301,L]
#skip wordpress site which starts with blog
RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Resources