2012-02-08 77 views
0

我很難用.htaccess重寫一個簡單的URL。簡單的URL重寫問題

我試圖重寫URL是:

http://www.domain.com/index.php?page=PAGE_NAME 

我想PAGE_NAME域之後直接,例如,如果PAGE_NAME是博客:

http://www.domain.com/blog 

目前我已嘗試以下沒有成功:

RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1 

所有幫助真的很感激。

在此先感謝。

我現在的.htaccess文件:

<IfModule mod_rewrite.c> 

Options +FollowSymLinks 
Options +Indexes 
RewriteEngine On 

ErrorDocument 404 /search.php?page=notfound 

# Add WWW to URL 
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC] 
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301] 

# Remove trailing slashes from end of URL 
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC] 
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301] 

# Rewrite main page URLs 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC] 
RewriteRule^%1? [L,R=301] 
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC] 

# Rewrite download URLs 
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1 

# Rewrite page navigation links 
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2 

# Rewrite article URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2 

# Remove file extension from PHP files 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA] 

</IfModule> 

回答

0

嘗試添加下列到.htaccess文件在您的網站的根目錄。

RewriteEngine on 
RewriteBase/

#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC] 
RewriteRule^%1? [L,R=301] 

#send request to index.php 
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC] 
+0

我只是發現了一個問題,現在所有的網頁被定向到index.php,例如,如果從index.php來search.php中去,index.php爲發送一個錯誤說「找不到網頁」 – Cristian 2012-02-08 21:18:33

+0

@Cristian我編輯了幾次解決方案:你有上面的版本?如果沒有,你可以使用最新的。如果是的話,上面的'RewriteRule'與'search.php'不匹配,不應該導致你描述的結果。你的.htaccess中有沒有其他規則? – 2012-02-08 21:22:38

+0

我不知道如何做到這一點,但問題是,domain.com/XXXX之後的所有內容都被髮送到index.php,我如何使它只被引導到index.php,如果它是domain.com /?page = XXXX? – Cristian 2012-02-08 21:23:30