Subdomains rewrite .htaccess - .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]

Related

Can .htaccess rules be cleaned-up?

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]

htaccess code remove www and id and php in sub directory

with this code and clean URL like (example.com) I could add www and remove id in url.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But now my root of website is in a folder.
so my new url is www.examole.com/newfolder
how can add newfolder to top code?
You can use this .htaccess in /newfolder:
RewriteEngine On
RewriteBase /newfolder/
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

I have 3 rules on .htaccess that is not function at same time Friendly URLs, Force HTTPS and Not WWW

Friendly URLs
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ?url=$1
Forcing 'https://'
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} !'"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1 [L]
Suppressing www. at the beginning of URLs
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
I'm using CloudFlare and when I activate all, it's return a loop.
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ?url=$1 [L,QSA]
Make sure to test this after clearing your browser cache.

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]

Resources