2014-10-22 61 views
0

我想我不明白.htaccess如何重寫工作,所以我要求你的幫助。我需要將http://www.mywebsite.com/index.php?name=first更改爲http://www.mywebsite.com/first。 這可能嗎? 我現在的.htaccess文件的內容是:什麼是適合我網站的.htaccess rewriterule

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^/(.*)$ /index.php?menu=$1 [L] 

顯然它不工作。 .htaccess文件的內容是什麼?

+0

你也應該[檢查你的'.htacess'文件是否已經加載](http://httpd.apache.org/docs/2.0/howto/htaccess.html#when)。如果您有root權限,請考慮[根本不使用'.htacess'文件](http://httpd.apache.org/docs/2.0/howto/htaccess.html#when) – Max 2014-10-22 10:04:12

回答

1

爲什麼不妳的.htaccess使用

<IfModule mod_rewrite.c> 

RewriteEngine on 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule 
^(.*)/?$ index.php?page=$1 [L] 

</IfModule> 

然後你index.php你將返回相應的頁面:

# SELECT WHICH PAGE TO DISPLAY 
function getPage($showPage) { 
    $modules = array(#never name the slugs as same as the php pages 
    '/sign-in/i' => 'register.php', 
    '/signout/i' => 'logout.php', 
    '/staff/i' => 'admin.php', 
    '/products/i' => 'blank.php', 
    ); 
    foreach ($modules as $slug=>$phpmodule) { 
     if(preg_match($slug, $showPage)) { 
      return $phpmodule; 
     } 
    } 
    return 'blank.php';#in case module not found display 404 page 
} 

if (isset($_GET['page'])) { 
require_once (getPage($_GET['page'])); #include the appropriate module file 
} 

有很多方法可以做到。我希望這給你一個想法,你可以開始,如果我的答案已被用於你,請投票或接受答案。

1

試試這個:

Options All -Indexes 

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*) index.php?name=$1 [L] 
+0

這很好用! – kotsios 2014-10-22 10:05:52

+0

另一個小東西......大多數時候只有這個變量(名稱),但有時我有另一個變量(index.php?name = $ 1&var = $ 2)。我怎麼能說'.htaccess什麼時候去尋找一個變量和什麼時候變量? – kotsios 2014-10-22 10:29:22

0

的只是把這個在您的.htaccess文件

<IfModule mod_rewrite.c> 

RewriteEngine on 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule 
^(.*)/?$ index.php?name=$1 [L] 

</IfModule> 
+0

對於更多變量試試這個:RewriteRule^browse /([^ /] +)/([^ /] +)/?$?page = browser&make = $ 1&ser = $ 2 [NC,L] – 2014-10-22 10:38:49

+0

你的情況:RewriteRule^([^ /] +)/([^ /] +)/?$ index.php?&name = $ 1&var = $ 2 [NC,L] 這對您有幫助嗎? – 2014-10-22 10:42:04

+0

大多數時候只有這個變量(名稱),但有時我有另一個變量(index.php?name = $ 1&var = $ 2)。我怎麼能說'.htaccess什麼時候去尋找一個變量和什麼時候變量? – kotsios 2014-10-22 11:06:54

相關問題