2012-12-25 168 views
3

我真的不知道在Apache中重寫太多,但是我的.htaccess文件存在問題。將.php重定向到無擴展名並使URL不帶.php

原來的.htaccess是

Options +FollowSymLinks 

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L] 


RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

# Redirect non-www to www: 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


# Unless directory, remove trailing slash 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L] 

# Redirect external .php requests to extensionless url 
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L] 

# Resolve .php file for extensionless php urls 
RewriteRule ^([^/.]+)$ $1.php [L] 


ErrorDocument 404 /404.html 

第一改寫爲個性化網址,另一個被重定向非www會www和後.PHP分機少和文件重定向.PHP分機少,

我面對的問題是,如果我請求www.domain.com/ttt.php,如果ttt不存在,那麼它會將我重定向到http://www.domain.com/profile?id=ttt.php 另外,我想重寫爲php文件而不是其他類型,請說如果我有ttt.png文件,並打開www.domain.com/ttt,它會打開圖像而不是顯示404錯誤。

與其他類型的文件類似。如果名稱很常見,它首先打開.txt文件。

感謝您對此進行的任何更改,嚴重的是我不想讓我的網站混亂。

回答

1

這條規則:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L] 

需要是在最後。

這條規則:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

需要[L]標誌和需要更換RewriteRule ^([^/.]+)$ $1.php [L]。然後,你可以複製這個與.png,並.txt和要處理所有其他附加信息替代.php,按優先順序:

Options +FollowSymLinks -Multiviews 

RewriteEngine on 

# Redirect non-www to www: 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


# Unless directory, remove trailing slash 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L] 

# Redirect external .php requests to extensionless url 
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L] 

# add extension if the php file exists: 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

# add extension if the png file exists: 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.png -f 
RewriteRule ^(.*)$ $1.png [L] 

# add extension if the txt file exists: 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.txt -f 
RewriteRule ^(.*)$ $1.txt [L] 

# add extension if the html file exists: 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html [L] 

# etc. 

# finally, route to profile.php if all else fails 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L] 

ErrorDocument 404 /404.html 

既然你路由到profile.php,在profile.php腳本,如果id參數不存在(意味着路由的URI需要是404),則需要重定向到/404.html

相關問題