I want to redirect directory /api/ and everything after it to HTTPS. Everything else must redirect to HTTP if a visitor tries to visit something with HTTPS.
I'd like WordPress to ignore the /api/ directory (and everything after it) from the current mod rewrite.
This is my current standard WordPress .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Have you tried adding :
RewriteCond %{REQUEST_URI} YourFolderName
RewriteRule ^(.*)$ https://www.example.com/YourFolderName /$1 [R,L]
(Taken from HERE for more info)
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Related
I have a wildcard ssl installed on my account (My host is Bluehost) and I followed the directions here from Bluehost's site to install the wildcard SSL. However, when I try and access the site at all it comes up with a 500 internal server error. I am using laravel framework right now for the subdomain, I don't know if that changes anything, but here is the .htaccess file for the main site (Which is a wordpress site):
# Custom subdomain .htaccess SSL + WordPress
RewriteCond %{HTTP_HOST} ^admin.mysite.com$
RewriteCond %{REQUEST_URI} !^/admin/public/
RewriteRule ^(.*)$ /admin/public/$1
RewriteCond %{HTTP_HOST} ^admin.mysite.com$
RewriteRule ^(/)?$ admin/public/index.php [L]
# End custom subdomain .htaccess
# BEGIN WordPress
AddHandler application/x-httpd-php70s .php
<IfModule mod_rewrite.c>
# Custom maindomain .htaccess WordPress
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
</IfModule>
# END WordPress
And here is the .htaccess file for the subdomain (the one with laravel installed):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Keep in mind too, I know nothing about .htaccess or how it works, I just did what the webpage said to do.
I suggest you to try below code for simple redirection HTTP to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This should work for your main domain and its sub-domain both. Now if you want to redirect Non-WWW domain to WWW then try below code;
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.domainname.com%{REQUEST_URI} [L,R=301]
Pretty straight forward, I have an address www.example.com, and I need to redirect it to a subfolder which the actual website resides in.
I figured out how to redirect a http protocol request to https but this one I couldn't from what I've read.
This is my htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/subfolder/ [R=301,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
AddHandler application/x-httpd-php55 .php .php5 .php4 .php3
Use a Negative lookahead in regex like:
RewriteRule ^(?!subfolder/)(.*) http://www.example.com/subfolder/$1 [R=301,NC,L]
I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL.
It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php.
# Custom Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteRule (.*) https://example.com/$1 [R,L]
</IfModule>
# End Custom Redirects
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
FYI The second block of redirects is required for Wordpress. I also cannot combine them into one single block or Wordpress will overwrite my changes.
When rewriting is configured in .htaccess context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match.
That means, when you request /wc-api/v2, it is not rewritten to HTTPS, because it fails your first RewriteCond, and so the rule in the next block rewrites it to /index.php internally.
Now the “next round” starts, with /index.php as input. RewriteCond %{REQUEST_URI} !^/wc-api/v2 now does result in “true”, because the REQUEST_URI is not /wc-api/v2 any more, it is now /index.php. Therefor, the RewriteRule is applied – and you are redirected to https://example.com/index.php
To avoid this, you must add another RewriteCond, which will prevent the rule from being applied for the REQUEST_URI /index.php as well:
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://example.com/$1 [R,L]
I got an error on my server with the suggestion above so modified it slightly and found that this worked for me:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://www.example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hope it works for you.
I am trying to create virtual subdomains using .htaccess and wordpress.
But it won't seem to work properly. Using wordpress I get urls like:
foo.eu/test-pagina/?lang=fr
I managed to rewrite this to what i want:
fr.foo.eu/test-pagina/
But when this url is rewritten the page returns an 500 internal server error.
I also made a wildcard for the subdomains in my dns configuration.
Do I have to make subfolders for each subdomain in my root ?
My current .htaccess code is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^([a-z]{2})\.hypnose\.eu$ [NC]
RewriteCond %{THE_REQUEST} \ /+([^\?]*)\?lang=([a-z]{2})&?([^\ ]*)
#RewriteRule ^ http://%2.hypnose.eu/%1?%3 [L,R]
RewriteCond %{QUERY_STRING} !(^|&)lang=
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.hypnose\.eu$ [NC]
RewriteRule ^(.*)$ /$1?lang=%1 [L,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Can anyone tell me what did I do wrong?
Have you added "fr" as your subdomain in the domain's control panel?
Add this in your htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/test-pagina/%1/$1 [L,NC,QSA]
I am looking to write 301 redirects for a site. I would like the redirect to go from http://www.ehlconsulting.com/our-services-basic-services/ to http://www.ehlconsulitng.com/services/.
My .htaccess file is as follow
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ehlconsulting.com
RewriteRule (.*) http://www.ehlconsulting.com/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Redirect 301 /our-services-basic-services/ http://www.ehlconsulting.com/services/
When you go to the old URL it doens't redirect. Instead it shows "Bots get the naked version"
You probably don't want to use Redirect and want to move the actual redirect above your wordpress rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ehlconsulting.com
RewriteRule (.*) http://www.ehlconsulting.com/$1 [R=301,L]
RewriteRule ^our-services-basic-services/(.*)$ http://www.ehlconsulting.com/services/$1 [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The "Bots get the naked version" thing doesn't have to do with your rules, it's probably WPEngine or something that's doing that by default, and you might be able to override that by using a ErrorDocument 404 in your htaccess file. The problem with using a Redirect along with wordpress' RewriteRule's is that they conflict with each other.