1

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]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Aghaie
  • 44
  • 7

2 Answers2

0

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]
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
0

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;
}
Aghaie
  • 44
  • 7
  • Post any code into your question itself by editing it. Also inform the answerers by commenting on the answers. And What is this code intended for? Use @username (like @Aghale) in comments to notify the user. – ThinkingMonkey Feb 14 '12 at 01:47