Dang Tung Lam
Dang Tung Lam

Reputation: 95

domain regex for .htaccess

Firstly, sorry for my bad English. I want config my .htaccess to rewrite URL.

example.com/company1.com

instead example.com/sub=company1.com

My .htaccess now:

RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1

I was search in stackoverflow. If i using (.*) regex for all charaters or ([a-z\.]+) for include "dot" character in domain string ( company1.con), my skin was broken. My temporary solution is use ([a-z_]+) with http://example.com/company1_com instead http://example.com/company1.com It's bad solution :( So, please give me regex for this problem. Thanks.

Upvotes: 2

Views: 212

Answers (3)

Jon Lin
Jon Lin

Reputation: 143966

You need to prevent the index.php from looping:

RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]

You could also do a check for existing resources first. Since index.php exists, that would also break the loop. This would make it so if you're requesting static content like javascript or css, it won't get routed through index.php:

RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]

Upvotes: 0

Arne
Arne

Reputation: 1900

Rewriting for Apache is described in mod_rewrite.

For you, as long as you ignore possible GET-parameters or paths, it should be

RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]

I guess it was broken because either you were missing the "/" before index.php, there is a longer path in GET ( example.com/company1.com/css/style.css ) or you submit a form ( example.com/company1.com?a=foo&b=bar ).

Upvotes: 1

Oussama Jilal
Oussama Jilal

Reputation: 7749

Try this one

RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1

Upvotes: 0

Related Questions