簡體   English   中英

如何刪除和重定向所有.php擴展名,重定向“ www”以及刪除斜杠?

[英]How can I remove and redirect all .php extensions, redirect “www”, and remove trailing slashes?

我一直在嘗試從網站上的所有頁面中刪除“ .php”,同時還使用.htaccess從URL中刪除所有后綴斜杠和“ www”。 我已經可以同時使用其中的一兩個,但不是全部。 我真的很感謝您的幫助。

例如:

  • example.com/至example.com
  • 從www.example.com到example.com
  • example.com/index.com
  • example.com/index.php到example.com
  • 從example.com/chatroom.php到example.com/chatroom
  • example.com/directory/到example.com/directory

我想確保將舊的URL重定向到新的URL,並且沒有與PHP文件共享名稱的目錄。 在我的.htaccess文件中,我還有阻止特定引用程序並指定403和404錯誤的代碼。 非常感謝你!

我今天對尾隨的斜線有同樣的疑問。 這就是我最終在.htaccess文件中解決它的方式(重要的是DirectorySlash和第一個重寫規則):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Do not automatically add a slash for existing directories
DirectorySlash Off


#No trailing slash. This means that an URL entered with trailing / will change to an URL without trailing /
#The !-d rule can be included so that any URLs to existing directories are not changed
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

#Allow URL's which do not have the php file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^  %{REQUEST_FILENAME}.php [L]


#Redirect any file which does not exist to the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [R=301,L]

</IfModule>

因此,如果您輸入網址為example.com/index/,它將重定向到example.com/index。 如果您鍵入example.com/chatroom(並且存在chatroom.php),則URL中仍然包含example.com/chatroom,但是您的PHP腳本會收到example.com/chatroom.php

您需要添加的兩種情況是將index.php重定向到/並刪除www

我所包含的(並且我不知道您是否需要)是最后一條規則,它將任何不存在的文件重定向到index.php

使用htaccess URL重寫隱藏PHP

面向初學者的URL重寫

如何在PHP中重寫URL?

URL重寫工具

檢查以上網址。 在這些教程中,您可以了解如何重寫URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM