Jump to content

[SOLVED] Mod Rewrite With PHP


fri3ndly

Recommended Posts

Hi Guys

 

I have a website that currently displays products mod rewritten in the following way:

 

hxxp://www.mydomain.co.uk/content/page-item/type-fancydress/id-125/item-theitemtitle.html

 

which I want to display like this (without variables in the URL):

 

hxxp://www.mydomain.co.uk/item/fancydress/125/theitemtitle.html

 

Would this be easy enough to change? Below is the htaccess and PHP script I use:

 

HTACCESS:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*)\.html index.php?rewrite=$1 [L]


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.co.uk [nc]
rewriterule ^(.*)$ http://www.mydomain.co.uk/$1 [r=301,nc]

 

PHP:

function mod_rewrite( $request, $array_delim, $pair_delim )
{
global $_GET, $HTTP_GET_VARS, $_REQUEST;
$value_pairs = explode( $array_delim, $request );
$make_global = array();

foreach( $value_pairs as $pair )
{
$pair = explode( $pair_delim, $pair );
$_GET[$pair[0]] = $pair[1];
$_REQUEST[$pair[0]] = $pair[1];
$HTTP_GET_VARS[$pair[0]] = $pair[1];
}
}
// MOD REWITE ////////////////////////////////
#if we have a query_string
if( $_GET['rewrite'])
{
$request = $_GET['rewrite'];
mod_rewrite( $request, '/', '-' );

# if you have register_globals enables uncomment the following
# extract( $_GET, EXTR_OVERWRITE );
}

Link to comment
https://forums.phpfreaks.com/topic/119647-solved-mod-rewrite-with-php/
Share on other sites

Hi Guys

 

I have a website that currently displays products mod rewritten in the following way:

 

hxxp://www.mydomain.co.uk/content/page-item/type-fancydress/id-125/item-theitemtitle.html

 

which I want to display like this (without variables in the URL):

 

hxxp://www.mydomain.co.uk/item/fancydress/125/theitemtitle.html

 

Would this be easy enough to change?

If your url is always going to like page/type/id/item.html then yes, however if your url varies then no. Is you url always going to be in the above format?

You'll have to let mod_rewrite set your $_GET variables, rather than PHP (which means dropping your mod_rewrite function). So your mod_rewrites will become

 

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

 

# matches: item/fancydress/125/theitemtitle.html

RewriteRule /([a-z]+)/([a-z]+)/([0-9]+)/([a-z]+)\.html index.php?page=$1&type=$2&id=$3&item=$4 [NC,L]

 

# matches: item/fancydress/theitemtitle.html

RewriteRule /([a-z]+)/([a-z]+)/([a-z]+)\.html index.php?page=$1&type=$2&item=$3 [NC,L]

 

# matches: item/fancydress.html

RewriteRule /([a-z]+)/([a-z]+)\.html index.php?page=$1&type=$2 [NC,L]

 

 

Options +FollowSymlinks

RewriteEngine on

rewritecond %{http_host} ^mydomain.co.uk [nc]

rewriterule ^(.*)$ http://www.mydomain.co.uk/$1 [r=301,nc]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.