Reputation: 1757
I am using the following url to bring in all my page templates:
www.example.com/tournament/index.php?view=*
I want to rewrite this to
www.example.com/tournament/*
so
www.example.com/tournament/index.php?view=profile
becomes www.example.com/tournament/profile
and
www.example.com/tournament/index.php?view=profile&id=testUser
becomes www.example.com/tournament/profile/testUser
.
I currently have tried:
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
but this is not working. I have RewriteEngine On
as well.
The following file structure is:
|public
|->tournament
|->templates
|-> profile.php
|->index.php
|->.htaccess (with rewriting in)
Thanks in advance.
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";
?>
Upvotes: 2
Views: 198
Reputation: 41219
The following rule can handle both of your urls :
RewriteEngine on
RewriteCond %{THE_REQUEST} /tournament/index.php\?view=([^&]+)&id=([^&\s]+) [NC]
RewriteRule ^ /tournament/%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:tournament/)?([^/]+)/?(.*)/?$ /tournament/index.php?view=$1&id=$2 [NC,L]
Upvotes: 1
Reputation: 19016
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]
Upvotes: 1