Forum Moderators: phranque

Message Too Old, No Replies

Real URL to subdomain?

mod_rewrite, RewriteRule, subdomain, htaccess

         

ghost

11:42 am on Feb 22, 2008 (gmt 0)

10+ Year Member



hello everyone,
In my case, I want to hide real URL (www.domain.com/blog.php?user=anyone&post=anything)
rewrite to blog.domain.com/anyone/?anything
but I can only change
www.domain.com/blog.php?user=anyone to www.domain.com/anyone/

my .htaccess is
RewriteEngine On
DirectoryIndex index.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ blog.php?user=$1 [L]

how can I hide real URL domain.com/blog.php?user=anyone&post=anything to blog.domain.com/anyone/?anything

please help me.

Regards,
Bayarkhuu Amar

jdMorgan

5:13 pm on Feb 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just use two rules, most-specific first:

DirectoryIndex index.php
RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /blog.php?user=$1&post=$2 [L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /blog.php?user=$1 [L]

You might also consider making the URL-path patterns more specific in order to avoid the costly and slow "file or directory exists" checks. For example, if none of your blog post URLs contain a "filetype" you could skip the "exists" checks if the final URL-path-part contains a period indicating the presence of a "filetype" on the requested URL:

DirectoryIndex index.php
RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/.]+)/?$ /blog.php?user=$1&post=$2 [L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /blog.php?user=$1 [L]

That would avoid running the "exists" checks for robots.txt, yourlogo.gif, and even blog.php itself, none of which need to be rewritten. This could significantly speed up your server if you have a busy site.

Jim