Subdomain rewrite .htaccess - .htaccess

I have the follow .htaccess and it work good.
from http://us.domain.org/detail.php?id=1234 to
http://us.domain.org/category/city/computer-1234
but if I add:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /detail.php?id=$1 [L,QSA]
It does not work..How can I do?

from http://us.domain.org/detail.php?id=1234 to http://us.domain.org/category/city/computer-1234
Change your last rule to:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/-]+)-([^/]+)/?$ /detail.php?id=$4 [L,QSA]

Related

How force www except url

Use code force https with www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
How do I make the url below work both with WWW and without WWW
https://example.com/license/get_license.php
https://example.com/license/verified_license.php
Resolution
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/license/get_license\.php$
RewriteCond %{REQUEST_URI} !^/license/verified_license\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Credits: Hosting Webfaction

How can I write my redirect so it does not show the directory?

If I'm visiting my website for the first time then it always displays the directory that I'm pointing my site to. For example, if I visit example.com, it redirects to example.com/site.2014/. If I remove the directory from the URL, and revisit, it doesn't show up anymore. Can anyone explain why this happens? I have the following information in my htaccess file:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ site.2014/ [L]
You can use this code:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1 [L]
Make sure to test in a new browser.

subdomains linked to different files using htaccess

I'm trying to create redirect to different files for different subdomains, maybe domains in the future. This is based on ModX CMS.
I want to keep url the same, just run different file for each subdomain, passing some data. For domain.com it's index.php, for sub1.domain.com - sub1.php. Also I want to pass some data, you can see it in the example below:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub1.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub2.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub3\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub3.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub4\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub4.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub5\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub5.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub6\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub6.php?q=$1 [L,QSA]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I tried to # last 3 lines, but it didnt make any difference. I've got more lines in htaccess, but they are #. Subdomain I create in control panel of my server, all subdomains are ponted to the same directory (public_html).
Right now I'm not sure passing data works, and when I go to sub1.domain.com apache redirect me to www.domain.com/sub1.php, thich is wrong.
How do I link subdomains to their php files and keep url the same (sub1.domain.com)??
Remove redundant stuff and keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub1.php?q=$1 [L,QSA]

How to make this with htaccess?

How to make this with htacess:
http://example.com/category => /index.php?action=category
http://example.com/category?query=string => /index.php?action=category&query=string
http://example.com/category/subcategory => /index.php?action=category/subcategory
http://subdomain.example.com => /index.php?action=subdomain
http://subdomain.example.com/category/subcategory => /index.php?action=subdomain/category/subcategory
This is My current code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?$ /index.php?action=$1 [L]
The below rewrite rules will rewrite:
http://subdomain.domain.com => /index.php?action=subdomain/
I suggest you to handle that in your index.php to avoid extra rewrite rules.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(subdomain)\. [NC]
RewriteRule ^(.*)$ /index.php?action=%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(domain)\. [NC]
RewriteRule ^(category)(^/.*)?$ /index.php?action=$1$2 [L,QSA]
Thanks for helping.
.htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ /index.php?action=$1 [L]
index.php code:
$domain = "domain.com";
if($_SERVER["HTTP_HOST"] != $domain)
{
$subdomain = str_replace(".$domain","",$_SERVER["HTTP_HOST"]);
$_GET['action'] = (isset($_GET['action'])) ? "$subdomain/".$_GET['action']:$subdomain;
}

virtual subdomain

i want this:
http://100.mydomain.ir ---> http://mydomain.ir?option=user&id=100
i try this, but do not work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).mydomain.ir$ [NC]
RewriteCond %{HTTP_HOST} !^(www|mail).mydomain.ir$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).mydomain.ir$ [NC]
RewriteRule ^(.*)/$ ?option=user&id=%2 [R=301,L]
what is my correct .htaccess code?
Use mod_rewrite to achieve this.

Resources