Htaccess冲突一些网址不起作用

时间:2015-10-11 15:23:12

标签: apache .htaccess mod-rewrite friendly-url

我有这个htaccess文件现在问题是......我希望http://twekr.com/profile.php?user=aniket成为http://twekr.com/aniket .... 我也希望twekr.com/login.php成为twekr.com/login 但只有一条规则适用于以下行...要么profile.php规则工作,要么登录.php规则有效......在htaccess文件中有什么问题吗?

 Options +MultiViews -MultiViews -Indexes
 RewriteEngine On
 <IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access 1 year"
 ExpiresByType image/jpeg "access 1 year"
 ExpiresByType image/gif "access 1 year"
 ExpiresByType image/png "access 1 year"
 ExpiresByType text/css "access 1 month"
 ExpiresByType text/html "access 1 month"
 ExpiresByType application/pdf "access 1 month"
 ExpiresByType text/x-javascript "access 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType image/x-icon "access 1 year"
 ExpiresDefault "access 1 month"
 </IfModule>
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^\.]+)$ $1.php [NC,L]

 RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
 RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
 errorDocument 400 http://www.twekr.com/oops.php?400
 errorDocument 401 http://www.twekr.com/oops.php?401
 errorDocument 404 http://www.twekr.com/oops.php?404
 errorDocument 500 http://www.twekr.com/oops.php?500
 RewriteRule ^post/([a-zA-Z0-9_-]+)$ post.php?id=$1
 RewriteRule ^post/([a-zA-Z0-9_-]+)/$ post.php?id=$1
 RewriteRule ^read/([a-zA-Z0-9_-]+)$ read.php?id=$1
 RewriteRule ^read/([a-zA-Z0-9_-]+)/$ read.php?id=$1
 <ifModule mod_gzip.c>
 mod_gzip_on Yes
 mod_gzip_dechunk Yes
 mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
 mod_gzip_item_include handler ^cgi-script$
 mod_gzip_item_include mime ^text/.*
 mod_gzip_item_include mime ^application/x-javascript.*
 mod_gzip_item_exclude mime ^image/.*
 mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
 </ifModule>

1 个答案:

答案 0 :(得分:1)

如果您使用以下网址会更好:http://twekr.com/user/anikethttp://twekr.com/profile/aniket,因为如果将来有名为login的用户可能会遇到麻烦。

如果您确定不会有任何名称可能与服务器上现有php文件冲突的用户,则可以使用以下代码:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /profile\.php\?user=([^&\s]+) [NC]
RewriteRule ^profile\.php$ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
# Use the following if there is only login.php
# RewriteRule ^login/?$ /login.php [NC,L]
# Use following to rewrite all php files to be extensionless
RewriteRule ^.*$ $0.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /profiles.php?user=$0 [QSA,L]

如果您确实使用了有关使用http://twekr.com/user/aniket作为网址结构的建议;然后规则是:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /profile\.php\?user=([^&\s]+) [NC]
RewriteRule ^profile\.php$ /user/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/(.+)$ /profiles.php?user=$1 [NC,QSA,L]