Can .htaccess rules be cleaned-up? - .htaccess

I am helping with a project and noticed the following rules in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-projectdomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-projectdomain.com$
RewriteRule (.*)$ https://www.new-projectdomain.com/$1 [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I'm not an expert on this but it looks somewhat redundant, I just can't put my finger on it.Can it be simplified?

You can use a single rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-projectdomain\.com$ [NC]
RewriteRule ^ https://www.new-projectdomain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Related

combine this two conditional format in one htaccess

i just want to know how to combine this two conditional format in one htaccess
SSL Https active Force non-wwwRewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Hide extension
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
maybe like this?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

htaccess redirect http and www to https non-www with one rewrite rule not working so

I had the following .htaccess code working with 2 RedirectRules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
But I want to make an optimized solution.
I came up with this based on my previous htaccess, but this is not working, and I would like to know why?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Another variation (preferred), not working:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Browser gives "Corrupted Content Error" message.
You may be able to use this combined rule:
## remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to use a new browser for testing the change.

I want to rewrite subdomains and folders to params

I like to rewrite my subdomain site (wildcard) and when exists folders to params for the index.php file.
So
www.domain.com
www.domain.com/js/file.js
www.domain.com/css/file.css
www.domain.com/images/file.jpg
should be preserved!
And these should change to parameters for index.php
sub.domain.com/
to
www.domain.com/index.php?key1=sub
sub.domain.com/2015
to
www.domain.com/index.php?key1=sub&key2=2015
sub.domain.com/2015/12
to
www.domain.com/index.php?key1=sub&key2=2015&key3=12
Is this possible?
What I have sofar is
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L,NE]
Thank you very much!
So I rewritten some of #anubhava his htaccess to this
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)(/?)$ $1?page=%1&key2=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)/([^/][0-9]*)(/?)$ $1?page=%1&key2=$1&key3=$2 [QSA,L]
And it almost works!
This url:
http://agenda.gebruikmaar.nl
Becomes
http://www.gebruikmaar.nl/index.php?page=agenda
BUT :-(
This url:
http://agenda.gebruikmaar.nl/2015
Becomes:
http://www.gebruikmaar.nl/index.php?page=agenda&key1=201
So where's the last character (5) at?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ index.php?key1=%1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^([^/]+)/?$ index.php?key1=%1&key2=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?key1=%1&key2=$1&key3=$2 [QSA,L]

dynamic subdomains using .htaccess in php

i am trying to implement dynamic
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]
Now this .htaccess converts
http://mydomain.com/live/agent/index.php?agent_user_name=username to http://username.mydomain.com
and
http://mydomain.com/live/agent/forum.php?agent_user_name=username to http://username.mydomain.com/form/
However, there are other pages as well which i want to redirect to subdomain like
http://mydomain.com/live/agent/view_blog.php?agent_user_name=username&blog_id=19 this page should be read via subdomain something like http://username.mydomain.com/view_blog/19 etc and also
http://mydomain.com/live/agent/page.php?agent_user_name=username&content_id=19 this page should be accessed by http://username.mydomain.com/content/19 etc
Thanks
This should work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^content/([0-9]+)/?$ /live/agent/page.php?agent_user_name=%1&blog_id=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/([0-9]+)/?$ /live/agent/$1.php?agent_user_name=%1&blog_id=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]
Add the following below your current rules should achieve what you want.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/view_blog.php\?agent_user_name=(.*)&blog_id=(.*)\ HTTP
RewriteRule ^ http://%2.mydomain.com/view_blog/%3? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/page.php\?agent_user_name=(.*)&content_id=(.*)\ HTTP
RewriteRule ^ http://%2.mydomain.com/content/%3? [R,L]

Subdomains rewrite .htaccess

I have these rules, everything work good.
I just need to add this rule:
from it.domain.org/index.php?city=newyork to it.domain.org/product-usa-newyork
The only thing that change is "newyork". It can be also: lasvegas etc..
Try inserting these 2 rules before your existing rules:
#Rewrite from http://it.domain.org/index.php?city=dc to http://it.domain.org/product-usa-dc
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /product-usa-%1? [R=302,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product-usa-([^/]+)/?$ /index.php?city=$1 [L,QSA,NC]
Put it at the start
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /product-usa-%1 [R=302,L]

Resources