简体   繁体   English

URL重写:规则适用于某些.php页面,但不适用于其他页面。

[英]URL-Rewriting: Rules work for some .php pages, but not for others.

I have an .HTACCESS file which allows a user to write the username of a user, on my social networking site in the URL. 我有一个.HTACCESS文件,该文件允许用户在URL的我的社交网站上写入username的用户username

I first implemented these rules to easily access specific users profile_page 's by just modifying the URL, ie profile_page/freddy - Will take me to Freddy's profile page etc. And the rules were working. 我首先实施了这些规则,只需修改URL即profile_page/freddy即可轻松访问特定用户的profile_page将带我进入Freddy的个人资料页面,等等。这些规则正在起作用。

I also have bio.php which is unique for each user. 我也有bio.php ,这对每个用户都是独一无二的。 Therefore, I have written the exact same rules as I did for profile_page but just changed the file name. 因此,我编写了与profile_page完全相同的规则,但只更改了文件名。 However, this does not work, I get an 404 error. 但是,这不起作用,我收到404错误。

I have several pages similar to this, but the rules for profile_page are the only ones that work. 我有几个与此类似的页面,但profile_page的规则是唯一有效的。

.HTACCESS in full: .HTACCESS完整:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(me)?/?$ profile_page.php [L,NC]
RewriteRule ^profile(?:_page)?/([\w-]+)/?$ profile_page.php?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile_page.php?u=$1 [L,QSA]

RewriteRule ^(me)?/?$ photos.php [L,NC]
RewriteRule ^photos?/([\w-]+)/?$ photos.php?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ photos.php?u=$1 [L,QSA]

RewriteRule ^(me)?/?$ hearted_thoughts.php [L,NC]
RewriteRule hearted_thoughts([\w-]+)/?$ hearted_thoughts?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ hearted_thoughts?u=$1 [L,QSA]

RewriteRule ^(me)?/?$ favourited.php [L,NC]
RewriteRule favourited([\w-]+)/?$ favourited?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ favourited?u=$1 [L,QSA]

RewriteRule ^(me)?/?$ bio.php [L,NC]
RewriteRule bio([\w-]+)/?$ bio?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ bio?u=$1 [L,QSA]

Your rewriteRules are conflicting with one another because of the indentical rewrite patterns ( ^(me)?/?$ and ^([w-]+)$ ) in all rules. 由于所有规则中的重写模式相同( ^(me)?/?$^([w-]+)$ ),因此您的rewriteRules相互冲突。

edit your rules like this : 像这样编辑您的规则:

Options -MultiViews
RewriteEngine On
RewriteBase /
#--let existent files and dir pass untouched--#
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
################  
#--rewrite /me to /profile_page.php--
RewriteRule ^(me)?/?$ profile_page.php [L,NC]
###############
#--Rewrite /profile/foo to /profile_page.php?u=foo--#
RewriteRule ^profile(?:_page)?/([\w-]+)/?$ profile_page.php?u=$1 [L,QSA,NC]
###############
#--rewrite /bio/foo to /bio.php?u=foo--#
RewriteRule ^bio/(.+)$ /bio.php?u=$1 [NC,L]
###############
#--other rules--#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM