Reputation: 7058
I am trying to get page.php?title=foo
to change to /page/foo
. I've got it working to the point the page.php goes to /page/, but when I do page.php?title=foo
it rewrites the url to /page/title?title=foo
.
I'd like to remove the ?title=foo
part of the last url. Any ideas?
I'm new to this - I tried to follow tutorials but am unsure what the [OR]
and [NC]
(etc) mean.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} ^title=(.*)$ [NC]
RewriteRule ^page.php$ page/%1 [NC,L,R=301]
Upvotes: 0
Views: 192
Reputation: 315
You can do something like this:
RewriteEngine on
RewriteRule page.php - [L]
RewriteRule ^page/(.*)$ page.php/?title=$1 // $1 - content of ()
So, when user ask http://example.com/page/foo
, Apache will open him http://example.com/page.php/?title=foo
. But adressbar is still with http://example.com/page/foo
Upvotes: 1
Reputation: 517
For all of my devs, I forward all requests to the index.php
, I exclude all css, js, jp(e)g, pdf, zip etc.
.htaccess
DirectoryIndex index.php
#ErrorDocument 404 /index.php?page=404
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(.+)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteCond %{REQUEST_URI} !\.(ico|docx|php|png|css|jpe?g|js|pdf|txt|mp3)$
RewriteRule ^(.*)$ index.php
Then in your PHP boot code,
Setup vars to give content and use $_GET
later if you want....
Exemple :
<?php
function rewrite(){
$link = $_SERVER['REQUEST_URI'];
$params = array("base_url", "locale", "page", "action", "par1", "par2", "par3", "par4");
$extraParams = preg_match("/\?/i", $link, $extraParamsMatches);
if($extraParams){
$link = explode("?", $link);
$link = $link[0];
}
$link = explode("/", $link);
$i = 0;
$vals = array();
foreach($link as $key => $value){
if($params[$key] == "base_url") $value = $_SERVER['SERVER_NAME'];
//if(empty($value)) continue ;
$vals[$params[$key]] = $value;
if($i == sizeof($params)-1){
break;
}
$i++;
}
return $vals;
}
}
?>
IMPORTANT
YOU CAN EDIT THIS CODE TO IMPROVE SECURITY FOR EXAMPLE SANITIZE DATA BEFORE USE IT IN THE FUNCTION ;-)
if the requested page is not found physically or in array from php or database, you can send header('Location : ',true, 404);
and give your 404 page !!
Upvotes: 0