Problem With a php + htaccess. Trying to shorten URL's.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mynyhart
Forum Newbie
Posts: 3
Joined: Wed Sep 15, 2010 1:15 pm

Problem With a php + htaccess. Trying to shorten URL's.

Post by mynyhart »

http://www.myspace-help.com/Myspace-Backgrounds/1.htm
is the Final Product of what i was able to do with my script + .htaccess
Its a combanation of

htacess
[text]
RewriteRule ^(.*)/(.*).htm$ $1/index.php?page=$2 [L]
[/text]

php script

Code: Select all

<?php
require_once( dirname(__FILE__) . '/_config.php');
if(!isset($_GET['page'])):
header("HTTP/1.1 301 Moved Permanently");
header("Location: 1.htm");
exit();
endif;
$page = preg_replace("@[^0-9]@", "", $_GET['page']);
}
How do i properly turn what is normally http://www.myspace-help.com/Myspace-backgrounds/?=page1
to http://www.myspace-help.com/Myspace-Backgrounds1.htm


This is the whole code section of the script if it helps.

Code: Select all

<?php
require_once( dirname(__FILE__) . '/_config.php');

if(!isset($_GET['page'])):
header("HTTP/1.1 301 Moved Permanently");
header("Location: 1.htm");
exit();
endif;
$page = preg_replace("@[^0-9]@", "", $_GET['page']);

$_dir = end(explode('/', $start));

$_cache = 'categories';

if($_caching_on == 1){
require_once( dirname(__FILE__) . '/_encoded/cache_head.php');
}

else{
require_once( $_path . '/_inc/_encoded/'.$_cache.'.php');

if( $page > $pages ):

header("HTTP/1.1 301 Moved Permanently");
header("Location: 1.htm");
exit();

endif;
}

$_rep = array('-'=>' ','_'=>' ','1'=>'','2'=>'','3'=>'','4'=>'','5'=>'','6'=>'','7'=>'','8'=>'','9'=>'');


$tags = ucwords(trim(strtr(current($SliceArr), $_rep))).', ';
 
$total = (count($SliceArr)-1);

for($i=0; $i<$total; $i++){

if($i != ($total - 1)){
$tags .= ucwords(trim(strtr(next($SliceArr), $_rep))).', ';
}else{
$tags .= ucwords(trim(strtr(next($SliceArr), $_rep)));
	}
}

include( $_path . '/_theme/header.php');
echo '<h1>'.$_dir.' Category </h1>'."\n";
echo '<div id="play">'."\n";
if(file_exists('before.php')) include('before.php');

foreach($SliceArr  as $OBJ):

$name = ucwords(trim(strtr($OBJ, $_rep)));

if(file_exists($_path.'/'.$_dir.'/'.$OBJ.'/thumb.jpg')){
$THUMB = 'thumb.jpg';
}

elseif(file_exists($_path.'/'.$_dir.'/'.$OBJ.'/thumb.png')){
$THUMB = 'thumb.png';
}

elseif(file_exists($_path.'/'.$_dir.'/'.$OBJ.'/thumb.gif')){
$THUMB = 'thumb.gif';
}

echo 

'<div id="cata" class="'.$_dir.'_category">'."\n".
'<p class="'.$_dir.'_category_title"><h2>'.$name.'</h2></p>'."\n".
'<a href="'.$_url.'/'.$_dir.'/'.$OBJ.'/"><img src="'.$_url.'/'.$_dir.'/'.$OBJ.'/'.$THUMB.'" border="0" class="nude"/></a>'."\n".
'</div>'."\n";

endforeach;

$num = 1;

echo
'</div>'."\n".
'<div id="pnav">'."\n".
'<span id="pages">Pages:</span>&nbsp;'."\n";


while( $num <=  $pages ):

if($page != $num)
echo '<a href="'.$num.'.htm" class="pnum">'.$num.'</a>'."\n";

elseif($page == $num)
echo $num."\n";

$num++;

endwhile;
echo '</div>'."\n";

if(file_exists('after.php')) include('after.php');

include( $_path . '/_theme/footer.php');

if($_caching_on == 1){
require_once( dirname(__FILE__) . '/_encoded/cache_foot.php');
}

?>

And my current htacess
[text]Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^(.*)/(.*).htm$ $1/index.php?page=$2 [L]
RewriteRule ^(.*)/(.*)/(.*).htm$ $1/2$/index.php?page=$3 [L]
ErrorDocument 404 /404.php
<ifModule mod_php5.c>
php_value default_charset utf-8
</ifModule>[/text]

Ill be on the forums all day reading up on it and checking out new stuff but if you want to msg me on msn it is Calm.to.catastrophe@gmail.com
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem With a php + htaccess. Trying to shorten URL's.

Post by Jonah Bron »

What exactly do you need help with? The first mod rewrite looks like it would work... Do you need help putting it into your current .htaccess file?
mynyhart
Forum Newbie
Posts: 3
Joined: Wed Sep 15, 2010 1:15 pm

Re: Problem With a php + htaccess. Trying to shorten URL's.

Post by mynyhart »

no
This is what i have now http://www.myspace-help.com/Myspace-Backgrounds/1.htm
im trying to figure out how to make it http://www.myspace-help.com/Myspace-Backgrounds1.htm

And the above is the codes i have to work with unless which im pretty sure im doing it backwards and there is a easyer way.
mynyhart
Forum Newbie
Posts: 3
Joined: Wed Sep 15, 2010 1:15 pm

Re: Problem With a php + htaccess. Trying to shorten URL's.

Post by mynyhart »

Ok so
[text]RewriteRule ^(.*)/(.*).htm$ $1/index.php?page=$2 [L]
RewriteRule ^(.*)/(.*)/(.*).htm$ $1/2$/index.php?page=$3 [L]
RewriteRule ^(.*)-(.*).htm$ $1/$2.htm [L][/text]

Got me
http://www.myspace-help.com/Myspace-Backgrounds/1.htm
http://www.myspace-help.com/Myspace-Bac ... unds/1.htm
http://www.myspace-help.com/Myspace-Backgrounds-1.htm

How do i get
http://www.myspace-help.com/Myspace-Bac ... unds-1.htm

I Also tried this formula but did not work
[text]RewriteRule ^(.*)-(.*)-(.*).htm$ $1/$2/$3.htm [L][/text]
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem With a php + htaccess. Trying to shorten URL's.

Post by Jonah Bron »

Try this:
[syntax]RewriteRule ^(.*)([0-9]+).htm$ $1/$2.htm [L][/syntax]
That should give you

http://www.myspace-help.com/Myspace-Backgrounds1.htm
Post Reply