I've been trying for the past hour to rewrite the following url using mod_rewrite in htaccess. Original url http://localhost/index.php?Part=main&page=download Desired url http://localhost/index.php?Part=/Main/Download
I do remember from the past that htaccess didn't work on on my pc for some reason.. Platform: Windows 7 Ultimate 64bit. Software: Apache - Xampp.
I also checked httpd.conf and I couldn't find anything wrong with it,it is set to "AllowOverride All"... Any idea what's the problem might be? And is it even possible to rewrite it the way I mentioned above (file.php?Part=/$1/$2 instead of file.php?Part=$1&page=$2)?
Thank you for your time, I really do appreciate it!
Pasted the following code in my htaccess and it has returned an error 500
Options +FollowSymlinks
RewriteEngine on
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]
If it can be done in PHP, please explain how. Contents of index.php:
<?php
session_start();
# Disable Notices
if(!file_exists('assets/config/install/installdone.txt')){
header("Location: assets/config/install/install.php");
exit;
} else {
# Get Database Information
require_once("assets/config/database.php");
require_once("assets/config/properties.php");
require_once("assets/config/afuncs.php");
# Define $getbase variable
$getbase = isset($_GET['Part']) ? $_GET['Part'] : "";
switch($getbase){
case NULL:
header('Location: ?Part=main');
break;
case "main":
$getslug = $mysqli->query("SELECT slug from ".$prefix."pages");
while($fetchslug = $getslug->fetch_assoc()) {
$slugarray[] = $fetchslug['slug'];
}
include("sources/structure/header.php");
include("sources/structure/theme/left.php");
include("sources/structure/theme/right.php");
include("sources/public/main.php");
include("sources/structure/footer.php");
break;
case "ucp":
include("sources/structure/header.php");
include("sources/ucp/main.php");
include("sources/structure/footer.php");
break;
case "admin":
include("sources/structure/admin/header.php");
include("sources/admin/main.php");
break;
case "gmcp":
include("sources/structure/header.php");
include("sources/gmcp/main.php");
include("sources/structure/footer.php");
break;
case "misc":
include("sources/misc/main.php");
break;
default:
include("sources/structure/header.php");
include("sources/public/main.php");
include("sources/structure/footer.php");
break;
}
}
$mysqli->close();
?>
You can re-write in whatever url style you wish, it also has regex in which you can pretty much do anything.
Drop this rule in the .htaccess file
Options +FollowSymlinks
RewriteEngine on
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]
In addition, set:
<Directory ...>
AllowOverride none
to
<Directory ...>
AllowOverride all
If it's the opposite the settings in .htaccess file will have no effect, it'll look for settings in the httpd.conf file.
If you don't want .htaccess involved or having difficulties with it you can insert the rule directly into your virtualhost for an example:
<VirtualHost *>
ServerName www.example.com
RewriteEngine on
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]
</VirtualHost>
Your rule:
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]
has too many spaces. Apache is kind of obtuse about how it parses directives, so when it sees a space it assumes a new parameter. So in your case, apache assumes that http://localhost/index.php?Part=/Main/Download$1
are the rewrite flags since it's the 3rd parameter of a RewriteRule
, and obviously, those aren't valid flags.
Additionally, you can't match against the query string in a rewrite rule, you need to match against the %{QUERY_STRING}
variable. You can then use the %
backreferences to reference groupings in that match. Try something like:
RewriteCond %{QUERY_STRING} ^Part=([^&]+)&page=([^&]+)
RewriteRule ^index\.php$ /index.php?Part=/%1/%2 [L,R]