drgm51600 2014-01-29 03:26
浏览 42
已采纳

单个.htaccess文件,允许虚荣URL和会话

I am trying to create an .htaccess file that does two things. Firstly, I need it to allow me to have vanity URLs that pass to a page called profile.php. Secondly, I want to be able to have sessions remain consistent regardless of whether the user types www. or not in the browser. I have both of these capabilities working in two separate .htaccess scripts. I need a single .htaccess file that will allow me to do both of these things.

This allows me to do the vanity url:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]

This allows me to have a single set of sessions without worrying about www.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^website\.com
RewriteRule ^(.*)$ http://www.website.com$1 [R=permanent,L]

Any help combining these into a single .htaccess file would be great! Thanks!

  • 写回答

1条回答 默认 最新

  • doulangbi6869 2014-01-29 03:37
    关注

    You want to redirect before you route. But your route needs to have the http://www.website.com removed otherwise there will be an implicit redirect:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
    RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [L]
    RewriteRule ^(.*)$ /profile.php?u=$1 [NC,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?