0
如何在我的.htaccess文件中一起完成所有這些操作?如何將所有請求路由到index.php,將不存在的文件路由到錯誤頁面,使用漂亮的URL並添加尾部斜線?
- 將所有傳入的請求到
- 路線不存在的文件index.php來一個錯誤頁面
- 重寫URL以漂亮的版本(不帶.php)
- 自動添加尾隨的斜槓搜索引擎優化的目的
我已經知道如何分開做這些,但我很難讓他們一起工作。這是我到目前爲止:
RewriteEngine on
RewriteBase /apps/louis/
#if file doesn't exist, try grabbing the .php version of it and continue on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [NC,N]
#if .php file doesn't exist, route to the index page with a 404 code and "error" param so index page knows to display error content
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?pageName=error [QSA,NC,R=404,L]
#if request name is not empty, route to the index page with a param of the page name so it knows which content to display
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*)$ index.php?pageName=$1 [QSA,NC,L]
#as long as the requested URL isn't an image or CSS file or script, tack on a trailing slash
RewriteCond %{REQUEST_URI} !\.(jpg|png|gif|css|js)$
RewriteCond %{REQUEST_URI} !(.*)/$
我在做什麼錯?我添加了評論來描述我認爲應該發生的事情,所以如果需要請糾正我。
非常接近!但是,你還可以請包括其他位,它應該重定向到index.php?pageName = $ 1(從我的帖子上面)。 – DecafJava
非現有文件可以轉到錯誤頁面或'index.php?pageName = $ 1'。否則,您需要有一組有效的頁面,您需要在.htaccess中列出 – anubhava
正確,但我會在哪裏擠入邏輯,告訴它將現有文件轉發到index.php?pageName = $ 1(其中$ 1是REQUEST_FILENAME)。您的解決方案將其直接發送到.php頁面而不是index.php頁面,該頁面是所有請求的單例處理程序 - 有效和錯誤。 – DecafJava