I tried the following but does not seem to work:
# IF HTTP_HOST != SERVER_NAME DO 403
RewriteCond %{HTTP_HOST} !%{SERVER_NAME} [NC]
RewriteRule .* - [F,L]
You cannot use a variable on RHS of RewriteCond. Use it like this:
RewriteCond %{SERVER_NAME}#%{HTTP_HOST} ^(?:www\.)?([^#]+)#(?!(www\.)?\1).* [NC]
RewriteRule .* - [F,L]
Related
Im trying to setup a redirect on a server from /index.html to the root domain.
Ive attempted this by using the below, the problem is instead of redirecting to the root, the code redirects to : domain.com/domain.com
Im sure its something simple, like missing a trailing slash, but for the life of me i cant see why this isnt working, any ideas ?
Code :
#index-and-WWW-Redirect
RewriteEngine on
#Redirect from naked ip
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xxx$
RewriteRule ^ https://example.com [L,R,NE]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule (.*) https://%{SERVER_NAME} [R,L]
# redirect everything to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
# set a variable for https or http
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
# end set a variable
RewriteCond %{HTTP_HOST} ^(www\.example\.com)(:80)? [NC]
RewriteRule ^(.*) %{ENV:proto}://example.com/$1 [R=301,L]
Options +FollowSymLinks
order deny,allow
How can I redirect HTTP to https include WWW using .htaccess?
Example :
redirect http://example.com to https://www.example.com
redirect http://www.example.com to https://www.example.com
redirect https://example.com to https://www.example.com
I'm trying
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
You can put the following code inside your .htaccess file
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This .htaccess file will redirect http://example.com/ to https://example.com/.
Code Explanation:
[NC] matches both upper and lower case versions of the URL
The X-Forwarded-Proto (XFP) header is a de-facto standard header for identifying the protocol
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 //If you want the condition to match port
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try in this. Hope this may help you.
Try this
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
APACHE
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
NGINX
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /srv/www/example.com/keys/ssl.crt;
ssl_certificate_key /srv/www/example.com/keys/www.example.com.key;
return 301 https://www.example.com$request_uri;
}
You can replace ssl on; directive with listen 443 ssl; as recommendation from nginx documentation.
I'm trying to rewrite urls if a few conditions are met and an if/else statement in .htaccess
If the
REQUEST_URI is empty or '/'
SERVER_NAME doesn't contain the word 'sam'
SERVER_PORT is 443.
Then prepend www. to the URL and append /website to the end of the URL.
Else if the
REQUEST_URI is not empty or not '/'
SERVER_NAME doesn't contain the word 'sam'
SERVER_PORT is 443.
Then prepend www. to the URL.
Here's what I have so far but I'm unsure if this is the correct order and even if it's correct as it's not working for me, no redirect takes place.
<If "%{REQUEST_URI} == '/' || %{REQUEST_URI} == ''">
RewriteCond %{SERVER_NAME} !sam
RewriteCond %{SERVER_PORT} 443
RewriteRule .* https://www.%{SERVER_NAME}/website [R=301,L]
</If>
<Else>
RewriteCond %{SERVER_NAME} !^sam
RewriteCond %{SERVER_PORT} 443
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</Else>
You can use, without other test:
RewriteCond %{SERVER_NAME} !sam
RewriteCond %{SERVER_PORT} 443
RewriteRule ^$ https://www.%{SERVER_NAME}/website [R=301,L]
RewriteCond %{SERVER_NAME} !sam
RewriteCond %{SERVER_PORT} 443
RewriteRule ^.+$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I want to change "+" to "-" in URL in php application by using htaccess
Example
http://hddir.com/videos/kodak+black
To
http://hddir.com/videos/kodak-black
My htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule ^videos/(.+)/(.+)/(.+)/(.+) search.php?q=$1&order=$2&type=$3&ptk=$4
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+) [NC]
RewriteRule ^ videos/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^videos/(.+)$ search.php?q=$1 [L,QSA,NC]
RewriteRule ^video/(.+).html$ watch.php?v=$1
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
#######Options -Indexes
ErrorDocument 404 /index.php
The following should do the trick by redirecting every domain with a plus in it permanently to the same domain but a minus instead of plus:
RewriteEngine On
RewriteRule ^(.*)\+(.*)$ /$1-$2 [L,R=301]
I try to rewrite my URLs from domain.tld/?country=XX&admin1=YY&city=ZZ to domain.tld/XX/YY/ZZ
I have tried
RewriteEngine on
RewriteRule ^([A-Za-z]{2,2})/(.*)/(.*)$ /?country=$1&admin1=$2&city=$3 [QSA]
Unfortunately it doesn't work. Have I missed something?
You can have these rules in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?country=([^\s&]+)&admin1=([^\s&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ /?country=$1&admin1=$2&city=$3 [QSA,L]