2015-10-20 48 views
2

下面的代碼將我們網站上的/ profiles /目錄中的所有URL從example.com/profiles/name/改爲example.com/name/,但我們還想刪除結尾的斜槓以進一步簡化生成的URL到更漂亮的頁面example.com/name - 就像現代社交媒體一樣。在Apache上沒有結尾斜槓的虛框URL

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /profiles/$1 [NC,L] 

這怎麼辦(安全地完成)?我們已經看到了幾個絆腳石的解決方案如果組合起來可能會起作用,但我們網站上的所有配置文件目前都有自己的物理目錄,而不是通過腳本即時組裝。

更新: @喬恩林在How to access directory's index.php without a trailing slash AND not get 301 redirect提供了一個解決方案,類似的情況 - 但是我們沒有弄清楚如何將其應用到我們的(如上所述)。

回答

0

通過添加How to access directory's index.php without a trailing slash AND not get 301 redirect(內部改寫了結尾的斜線回)由@建議喬恩霖代碼的一部分,我們實際上做這項工作:

# Vanity URLs 

DirectorySlash Off 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /profiles/$1 [NC,L] 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*[^/])$ /$1/ 

爲Gucci的時尚輪廓NET(位於/profiles/gucci/)現在可以在https://www.fashion.net/gucci訪問 - 無尾隨斜線!謝謝@ jon-lin !!!!

1

你可以嘗試做

RewriteRule ^(.*)/+$ $1 [R=301,L]

哪些應該對任何URL

+0

嘿@NooBskie!我們只是嘗試了一下,但是,在Google的Chrome上進行測試後,example.com/name /被重定向到一個包含整個內部目錄結構的URL,並且將example.com/name轉換爲Google搜索。 – haadaa

0

利用工作下列重定向:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} /(.+)/+$ 
RewriteRule^/%1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^.*$ /profiles/$0 [NC,L] 
+0

嘿@ hjpotter92 - 這與NooBskie的解決方案產生了相同的錯誤。 – haadaa

+0

@haadaa嘗試編輯的方法^ – hjpotter92

+0

謝謝!這又會重新導向Chrome上的Google搜索;在Safari和Firefox上,它將'example.com/name'重定向回'example.com/profiles/name'。 – haadaa

0

你需要禁用目錄削減

嘗試:

DirectorySlash Off 

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /profiles/$1 [NC,L] 
+0

嘿@Starkeen!這不起作用 - 'example.com/name'現在得到了「禁止 - 您無權訪問此服務器上的/ profiles/name」。 – haadaa