什么是适合我网站的.htaccess rewriterule

时间:2014-10-22 09:54:40

标签: php .htaccess mod-rewrite apache2.4

我想我不明白.htaccess如何重写工作,所以我在寻求你的帮助。我需要将http://www.mywebsite.com/index.php?name=first更改为http://www.mywebsite.com/first。 这可能吗? 我当前的.htaccess文件的内容是:

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

显然它没有用。 .htaccess文件的内容是什么?

3 个答案:

答案 0 :(得分:1)

试试这个:

Options All -Indexes

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

答案 1 :(得分: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
}

你有很多方法可以做到。我希望这可以让你了解你可以从哪里开始,如果我的答案对你有用,请投票或接受答案。

答案 2 :(得分:0)

将它放在你的.htaccess文件中

<IfModule mod_rewrite.c>

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

</IfModule>