[英].htaccess not removing .php extension from url
首先,我說我發現了很多腳本要放入.htaccess文件中,並記下了所有按我希望的方式工作的腳本。
因此,例如,如果我的htdocs文件夾中有一個名為test.php的文件,並且轉到了localhost / test.php,我希望在瀏覽器中將該文件重寫為localhost / test。
我找到了2個.htaccess文件來執行此操作,並在下面顯示了它們,但是它們的問題是,每當我嘗試使用表單將數據插入MYSQL數據庫時,我總是會得到空白字段。
這是第一個
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
這是第二個
# Turn on the rewrite engine
RewriteEngine on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]
然后搜索了幾個小時之后,我發現了一些腳本,它們的工作原理完全像一個咒語,然后昨天我去洗了澡,又回來了,它們沒有用。 (我刪除了舊的.htaccess文件,創建了一個新文件,重新啟動了mysql和apache,清除了瀏覽器數據等,並且.htaccess仍然無法正常工作),在我去淋浴之前,他們按照我的要求進行了工作。
這兩個以前工作(不,我沒有同時將它們都放在.htaccess文件中)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
第二個
# Options is required by Many Hosting
Options +MultiViews
RewriteEngine on
# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
因此,現在它們僅在我手動從網址中刪除.php但我不希望那樣做的情況下起作用,或者我不想通過更改.php擴展名來手動更改我的php文檔中的所有鏈接,而我我想使用其中任何一個,因為當我使用表單將數據插入MYSQL時,數據實際顯示出來,並且字段不是空白。
我還發現了一個添加了斜杠的斜杠,因此localhost / test.php變成了localhost / test /,但是它可以工作一次,現在不再工作了,但是如果我使用一種形式將數據插入MYSQL數據庫,字段也為空白。
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
就像我說的那樣,我發現並記下了那些按其應有的方式工作,但現在卻不起作用的人,有人知道為什么嗎? 或者如何調整他們以完全實現我想要的? 我從字面上看過去的一天都在尋找解決方法。 順便說一句,我在Windows 7上使用XAMPP
重寫時,發送的URL不是用來調用腳本的URL,但是您仍然可以訪問原始URL。 在index.php
,將$_SERVER['REQUEST_URI']
炸開?
,第一項是您的路徑,第二項(如果存在)是parse_str($exploded[1], $_GET);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.