Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

.htaccess: mod-rewrite; subdomain

I am working on a new website bud I want to create a nice looking urls for my users. I don't know anything of htaccess and could not find my solution on google.

I need to link:

  • user.mywebsite.com to>> mywebsite.com/users/user
  • user2.mywebsite.com/contact to>> mywebsite.com/users/user2/contact

Some links may not be linked like:

  • www.mywebsite.com may not link to mywebsite.com/users/www

Is this possibe to do with htaccess? If yes can someone explain it to me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using mod_rewrite, you can try:

RewriteEngine On

# the request URI doesn't already start with /users/
RewriteCond %{REQUEST_URI} !^/users/

# host doesn't start with www
RewriteCond %{HTTP_HOST} !^www.  [NC]

# host starts with something else
RewriteCond %{HTTP_HOST} ^([^.]+).mywebsite.com$  [NC]

# rewrite
RewriteRule ^(.*)$ /users/%1/$1  [L]

This will make it so when someone enters http://joe.mywebsite.com/some/page.html they will be served the file in /users/joe/some/page.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...