htaccess重写if语句

时间:2013-12-03 08:19:35

标签: .htaccess url rewrite

我正在为大学班级的twitter / facebook类型网站工作。不知何故,他们让教授在没有PHP,CSS,HTML,JavaScript,jQuery或Ajax知识的情况下教授这门课程。我一直试图重写我的网址,使它们看起来像twitter。我已将所有用户个人资料页面重写为:www.site.com/username:www.site.com/profile.php?name = username。但是,我还想重写我的登录页面,创建帐户页面等。目前它们是:www.site.com/login.html,www.site.com/createAccount.html。我希望在没有html的情况下重写。这是我目前的.htaccess文件。

    RewriteEngine  On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]

    RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]

**更新

我道歉我最终将所有文件切换到.php。但我仍然有一些问题。现在,我只想重定向特定的URL。例如:www.361orc.info/login应在内部重定向到www.361.orc.info/login.php。我似乎无法弄清楚以下代码有什么问题。它会重定向,但它会更改客户端URL。我希望它只是在内部重定向。这是我的.htaccess文件:

RewriteEngine  On

#I want this code to change .com/login.php to .com/login but only internally 
#the URL in the client's browser shouldn't change
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^login?$ login.php [L]

#Change the profile pages of users from .com/profile.php?name=user to .com/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L,QSA]

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情

# Rewrite User Profiles
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^profile.php$ /%1 [R=301,L]

# Rewrite Login
RewriteRule ^login.html$ /login [R=301,L]

# Rewrite Create Account
RewriteRule ^createAccount.html$ /createAccount [R=301,L]

答案 1 :(得分:0)

您非常接近,只是一个外部重定向规则,会将.html个文件重定向到没有.html扩展名的文件:

RewriteEngine  On
RewriteBase /

# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+?)/?$ $1.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ profile.php?name=$1 [L,QSA]