views:

58

answers:

3

The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example: The original URL:

www.mydomainname.com/en/piecework/piecework.php?piecework_id=11

Expected URL:

piecework.mydomainname.com/en/11

How to achieve it with mod_rewrite?

A: 

You need to define a rewrite rule (should be similar to this):

RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3

and put it in a .htaccess file under the main folder of you site

Full description of Rewrite rules here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

EDIT

Made a mistake in my rule, hopefully I corrected it.

Lex
Amarghosh
What does $3 or $2 mean?
Steven
Thankyou for pointing out the mistake. @Steven $2 means the second group of the reg-ex that you got from parsing the string with this rule ie.: the second (.*) in fact what you need is ([0-9]+) which is the third group referenced by $3
Lex
I have specify the rules(RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3) in .htaccess(in my site root), but when I access piecework.mydomainname.com/en/11, I got "Object not found".What's wrong?
Steven
Have you enabled .htaccess files in Apache configuration file?
Lex
A: 

I found a fairly well written crash course in doing so here:
http://articles.sitepoint.com/article/guide-url-rewriting

You'll need to specify the rules in .htaccess (in your site root).

Jagtesh Chadha
I have specify the rules(RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3) in .htaccess(in my site root), but when I access http://piecework.mydomainname.com/en/11, I got "Object not found".
Steven
A: 

I would suggest the following rule:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
TonyCool
I have specified the rules(RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]) in .htaccess(in my site root), but when I access piecework.mydomainname.com/en/11, I got "Object not found".What's wrong?
Steven
Sorry, I've made a misprint. Try it like this:RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
TonyCool