2013-02-15 140 views
3

我有這個網站,其中URL對頁面以前是這樣的:在.htaccess文件重定向的絕對路徑

請請注意,即使它們是.php文件,這些網址也沒有任何擴展名。這是一個位於域名根部的Wordpress網站。 WordPress的網站現在已經被移到一個文件夾中,但我想設置一個從所有以前的URL重定向到新的URL(出於搜索引擎優化的原因)。

到目前爲止,我發現了一個遠非完美的臨時解決方案。我所做的是我把這個在.htaccess文件(除去PHP的擴展):

# 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] 

然後我創建了不同的文件名爲一樣的前面的URL(用。 PHP擴展,然後由於我的.htaccess文件被刪除)。在每個文件的頂部,我插入了一個

<?php header('Location: THE_NEW_URL'); ?> 

它的工作原理,但它是醜陋的貝蒂! 最近,我遇到了一個錯誤,由於這個臨時的,愚蠢的修復我的問題。我試圖在文件夾的根目錄(稱爲test.php)中設置一個新頁面。這test.php的文件有一個樣式表,它鏈接到後面的方式(非常標準,我假設):

 <link href="css/style.css" rel="stylesheet" type="text/css" /> 

但test.php的文件無法找到這個樣式表(是的,我我確定我正確地上傳了它)。如果我去了源文件並單擊它,然後我可以看到,它試圖訪問

 css/style.css.php 

你不必成爲愛因斯坦就知道了,它是一個已經被提出當中我的.htaccess文件狼吞虎嚥。但我該怎麼辦?有沒有辦法讓我的.htaccess文件,所以我可以做線沿線的東西:

 Redirect from SPECIFIC_URL to A_NEW_SPECIFIC_URL 

凡不搞砸了所有的URL對我的域名。我喜歡在我的文件上有擴展名。我只需要修復這15個已移動的舊URL(沒有擴展名)。

我吮吸.htaccess文件,並找不到任何地方的任何教程。它很爛!我不知道如何改善它。我一直在實施我不明白的解決方案!?!

回答

2

我相信,這將滿足您的需求:

<IfModule mod_rewrite.c> 
     # enable URI rewrite 
     RewriteEngine on 
     # set virtual root 
     RewriteBase/
     # if request is for an actual file or directory on disk, 
     # then do not continue with rewrite engine 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteCond %{REQUEST_FILENAME} !-f 
     # setup rewrite table 
     RewriteRule ^page1$ page1.php [R=302,L] 
     RewriteRule ^page2$ page2.php [R=302,L] 
     RewriteRule ^page3$ page3.php [R=302,L] 
    </IfModule> 

您可能會發現通過簡單地增加略高於現有的條件,這兩個的RewriteCond線成功:如果只有15歲文件

RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
+0

1爲'的RewriteCond%{REQUEST_FILENAME} !-f',但不要在RewriteRule替換中轉義'\ .'。 – 2013-02-15 23:51:39

+0

oops - 良好的呼叫;編輯並刪除不必要的轉義。 – Nathan 2013-02-16 00:02:37

+0

Ps。我愛你! – Zeth 2013-02-16 00:14:01

1

,一個簡單的解決方案可以是硬編碼這些15個文件

RewriteEngine on 
RewriteRule ^/?oldfile1$ /new1/path1/file1.php [R,L] 
RewriteRule ^/?oldfile2$ /new2/path2/file2.php [R,L] 
RewriteRule ^/?oldfile3$ /new3/path3/file3.php [R,L] 
... 
+0

看起來它會起作用。內森只是更快,並與他的方法一起工作。謝謝,不過。 – Zeth 2013-02-16 00:17:18