htaccess不会为我的GET请求重写URL

htaccess不会为我的GET请求重写URL

本文介绍了htaccess不会为我的GET请求重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下网址导入我的所有页面模板:

I am using the following url to bring in all my page templates:

www.example.com/tournament/index.php?view=*

我要将其重写为

www.example.com/tournament/*

如此

www.example.com/tournament/index.php?view=profile变为www.example.com/tournament/profile

www.example.com/tournament/index.php?view=profile&id=testUser变为www.example.com/tournament/profile/testUser.

我目前已经尝试过:

RewriteRule ^tournament/([^/]*)/([^/]*)$ index.php?view=$1&id=$2
RewriteRule ^tournament/([^/]*)/([^/]*)$ index.php?view=$1&id=$2 [L]
RewriteRule ^tournament/([^/]*)/([^/]*)$ tournament/index.php?view=$1&id=$2
RewriteRule ^tournament/([^/]*)/([^/]*)$ tournament/index.php?view=$1&id=$2 [L]
RewriteRule ^tournament/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?view=$1&id=$2
RewriteRule ^tournament/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ tournament/index.php?view=$1&id=$2

但是这不起作用.我也有RewriteEngine On.

but this is not working. I have RewriteEngine On as well.

以下文件结构为:

|public
|->tournament
    |->templates
        |-> profile.php
    |->index.php
    |->.htaccess (with rewriting in)

谢谢.

编辑 index.php

edit index.php

<?php
    include '../../includes/functions.php';

    sec_session_start();

    if(isset($_SESSION['username'])) {
        $user = new user($db, $_SESSION['user_id'], $_SESSION['username'], $_SESSION['lastActive']);
    }

    include INCLUDESPATH . 'form_actions.php';
    include "headers/header.php";
    include "headers/navbar.php";

    echo '<div class="wrapper">';

    $view = 'index';
    if (!empty($_GET['view'])) {
        $tmp_view = basename($_GET['view']);

        if (file_exists("templates/{$tmp_view}.php")) {
            $view = $tmp_view;
            include "templates/{$view}.php";
        }
        else {
            include "errors/404.php";
        }

    }

    echo '</div>';

    include "headers/footer.php";
?>

推荐答案

由于您的htaccess位于tournament文件夹中,因此您可以使用这种方式

Since your htaccess is located in tournament folder, you can have it this way

RewriteEngine On
RewriteBase /tournament/

# Redirect /tournament/index.php?view=XXX to /tournament/XXX
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]

# Redirect /tournament/index.php?view=XXX&id=YYY to /tournament/XXX/YYY
RewriteCond %{THE_REQUEST} \s/tournament/index\.php\?view=([^&\s]+)&id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]

# Skip if existing file/folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Internally rewrite them back
RewriteRule ^([^/]+)$ index.php?view=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?view=$1&id=$2 [L]

这篇关于htaccess不会为我的GET请求重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:54