Please Help! 2 days wasted on php and htacess URL shortening!

is the Final Product of what i was able to do with my script + .htaccess
Its a combanation of

htacess


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

php script


<?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.

<?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>'."\
";
echo '<div id="play">'."\
";
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">'."\
".
'<p class="'.$_dir.'_category_title"><h2>'.$name.'</h2></p>'."\
".
'<a href="'.$_url.'/'.$_dir.'/'.$OBJ.'/"><img src="'.$_url.'/'.$_dir.'/'.$OBJ.'/'.$THUMB.'" border="0" class="nude"/></a>'."\
".
'</div>'."\
";

endforeach;

$num = 1;

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


while( $num <=  $pages ):

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

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

$num++;

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

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

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>

I Spent two days changing variables in each section, the htacess and the php script but to no avail.

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 <snip/>


tim,

Well, to do that for other images, either duplicate the code but replace ‘gif’ with (gif|jpg|png). The other cats will take care of themselves with the code I provided.

Regards,

DK

Thank you very much for the help.
how can make it work for jpg,png and all of the other Main cats like

myspace-help.com/Avatar/Guitar-Icon/View.php blah blah

also i tried the line u wrote to see if it will work for just that one instance and both
http://www.myspace-help.com/Background-Saint_Patrics_Day-19.html
and
http://www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
404

Looking at just these two questions, they can be handled the same way: Read the values of the query string for cat and layout (stripping off the .gif), ensure you’re looking at Background/view.php and redirect. E.g.,

RewriteCond %{QUERY_STRING} cat=([a-zA-Z_]+)&layout=([0-9]+)\\.gif$
RewriteRule ^Background/view\\.php$ background-%1-%2.html [L]

That’s the generic version as I think you want more than just St Patty’s Day and a 19 to be matched/redirected.

Regards,

DK

Ok, I have figured everything else out with my rewrites. I just need help with two last things

I know that these regex’s are not specific and will pick up anything. But im only doing it for testing. ill find better syntex’s if i can get what i want to get done on my site done.

# www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif to
# www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
RewriteRule ^([^/]+)-([^/]+)-([^/]+)/?$ $1/view.php?cat=$2&layout=$3 [L,NC]

First
This is the absolute URL that is generated by the graphics script.

I changed it to
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif.

This is good and im willing to keep it like this if what im asking for is impossible.

In each folder there is tons of images and dif extensions (jpg,gif,png). How can i turn

www.myspace-help.com/Background-Saint_Patrics_Day-20.jpg
www.myspace-help.com/Background-Saint_Patrics_Day-21.png
into
www.myspace-help.com/Background-Saint_Patrics_Day-19.html
www.myspace-help.com/Background-Saint_Patrics_Day-20.html
www.myspace-help.com/Background-Saint_Patrics_Day-21.html
or even
www.myspace-help.com/Background-Saint_Patrics_Day-19/

Also If that is not possible the 2nd problem i am having is this

www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif and
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
are both live, Meaning i have duplicate content.
How can i get
www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
to 301 to
www.myspace-help.com/Background-Saint_Patrics_Day-19.gif
or even www.myspace-help.com

I think that is structured thinking. Its two simple questions for two simple things i have spent way to long on testing my self that i need your help with.
Please if you can respond with a answer so i can end this 500 loop im doing with my brain trying to fix these 2 simple problems. Once agian
How can i do this
www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
to
www.myspace-help.com/Background-Saint_Patrics_Day-19.html

and how can i 301
www.myspace-help.com/Background/view.php?cat=Saint_Patrics_Day&layout=19.gif
to
www.myspace-help.com/Background-Saint_Patrics_Day-19.html

Edit:

Sorry, it looks like I edited your post rather than quoting! I’ve restored it, though, so I hope it’s a case of “no harm, no foul.” My apologies!

tim,

Okay, look at your RewriteRules and tell me how mod_rewrite is supposed to differentiate between them (other than first matched, first redirected). What you NEED to do is figure out a “marker” to allow mod_rewrite to distinguish between the ones that get redirected to DocumentRoot’s Directory Index (another reason for me to loathe the use of /?key=value) and Myspace-Backgrounds, etc. In other words, you can make one a “garbage collector” but the other one needs something like an MB at the start of the link (which can be discarded by mod_rewrite’s redirection) for Myspace-Backgrounds.

Regards,

DK

Im learning alot so far this is what i put together


RewriteCond %{HTTP_HOST} !^www\\.Myspace-Help\\.com$ [NC]
RewriteRule ^(.*)$ http://www.Myspace-Help.com/$1 [R=301,L]
# http://www.myspace-help.com/Backgrounds/?page=1 to Myspace-help.com/Myspace-Backgrounds-1.htm
RewriteRule ^(.*)/(.*)-(.*).htm$ $1/$2/?page=$3 [L,NC] 
RewriteRule ^([a-zA-Z-]+)/(\\d+)\\.htm$ $1/?page=$2 [L,NC] 
RewriteRule ^(.*)_(\\d+)\\.htm$ $1/$2.htm [L,NC] 
# Catagory Rewrite
RewriteRule ^([A-Za-z0-9-_]+)-(\\d+)\\.htm$ Myspace-Backgrounds/$1-$2.htm [L,NC]
# View.php Rewrite

it goes

Catagory in myspace backgrounds

And for View.php i have no clue
the string for it is

http://www.myspace-help.com/Myspace-Backgrounds/Abstract-Backgrounds/view.php?cat=Abstract-Backgrounds&layout=Abstract-1.jpg

im trying to get some sort of

http://www.myspace-help.com/Abstract-Backgrounds-Abstract-1.htm

but still nothing

RewriteRule ^([^/-]+)-([^/-]+)-([^/]+)/?$ /$1/view.php?cat=$2&layout=$3 [L]
RewriteCond %{THE_REQUEST} [1]+\ ([^&]+)/view\.php\?cat=([^&]+)&layout=([^&]+)\ HTTP/
RewriteRule /view\.php$ %1-%2-%3? [R=301,L]

301 and redirects to

Is there any way to Remove the gif,jpg,png and replace with a .html?


  1. A-Z ↩︎

THANK YOU SO MUCH!!!
I have finnally after so long figured it out. the perfect mod-rewrite for this script.

RewriteBase /
RewriteRule ^([^/-]+)-(\d+)\.php$ /$1/?page=$2 [L]
RewriteRule ^([^/-]+)-([^/-]+)-(\d+)\.htm$ /$1/$2/?page=$3 [L]
RewriteRule ^([^/-]+)-([^/-]+)-([^/]+)/?$ /$1/view.php?cat=$2&layout=$3 [L]
RewriteCond %{THE_REQUEST} [1]+\ ([^&]+)/view\.php\?cat=([^&]+)&layout=([^&]+)\ HTTP/
RewriteRule ^Background/view\.php$ %1-%2-%3? [R=301,L]

It shortens all urls and the last 3 lines turns my view.php into whatever i want and 301’s to it. Now i can go threw and fix all of the ([^/-]+) and such and make it more accurate


  1. A-Z ↩︎

RewriteCond %{QUERY_STRING} cat=([a-zA-Z_]+)&layout=([0-9]+)\.(gif|jpg|png)$
RewriteRule ^Background/view\.php$ background-%1-%2.html [L]

like that?

Finally, a specification - ALMOST! Structure your thinking/specification, if you please!

You have a list of Background - (guitar -) guys - 2 (extensions). Where do these get redirected?

The best thing to do is format your URIs in order that the sections are required to be present, i.e., {cat}-{layout}-{page}.{ext} so that {cat} must be present first, then {layout} is required to have {page} present, the {ext} can be used to redirect to … WHERE?

Regards,

DK


  1. COLOR=“Red” ↩︎

Also Thank you so much David for your help so far.

I Had to start from scratch again because the code i wrote only worked for one catagory and the php script i was useing did not like it


# Rewrites My main Catagories - Example myspace-help.com/Myspace-Backgrounds to Myspace-Backgrounds_1.htm
# And Myspace-help.com/Myspace-Comments/ to Myspace-Comments_1.htm
RewriteRule ^([a-zA-Z-_]+)/(\\d+)\\.htm$ $1/?page=$2 [L,NC] 
RewriteRule ^([a-zA-Z-_]+)_(\\d+)\\.htm$ $1/$2.htm [L,NC] 

This one works perfect. it turns all of my main catagories into what it needs to be

Now inside Myspace-Backgrounds_1.htm there is mutli catagories such as Animated Backgrounds and Yellow Backgrounds and myspace-comments/ Has things like Heart , Love, OMG, ZOMG

How do i turn these sub catagories dynamicly or even something where i have to copy the mod rewrite for each catagory like so

The Old code i used was


RewriteRule ^([a-zA-Z-_]+)-(\\d+)\\.htm$ Myspace-Backgrounds/$1-$2.htm [L,NC]

But when i tried
RewriteRule ^([a-zA-Z-_]+)-(\d+)\.htm$ Myspace-Comments/$1-$2.htm [L,NC]
Everything broke The myspace backgrounds still worked but the Myspace Comments categories did not.

And Finally after this problem is solved i still have my View.php Problem

I Used this code


# View.php Rewrite
RewriteRule ^([a-zA-Z-_]+)-([A-Za-z0-9-_]+).(gif|jpg|swf|flv|png)$ Myspace-Backgrounds/view.php?cat=$1&layout=$2.$3 [L,NC]

Which had the same effect as My Myspace-Backgrounds probleem

I tried
RewriteRule ^([a-zA-Z-]+)-([A-Za-z0-9-]+).(gif|jpg|swf|flv|png)$ Myspace-Comments/view.php?cat=$1&layout=$2.$3 [L,NC]

and The backgrounds one still worked but the one for Myspace-Comments did not.

And again here is all of my original Stings
This one seems to be working correctly


http://www.myspace-help.com/Myspace-Backgrounds/?page=1 
http://www.myspace-help.com/Myspace-Comments/?page=1 
To 
http://www.myspace-help.com/Myspace-Backgrounds_1.htm 
http://www.myspace-help.com/Myspace-Comments_1.htm

Next this is what needs to be solved as ive tried everything and still no luck


http://www.myspace-help.com/Myspace-Backgrounds/Abstract_Backgrounds/?page=1
http://www.myspace-help.com/Myspace-Comments/Spring/?page=1

To Something like
http://www.myspace-help.com/Myspace-Backgrounds-Abstract_Backgrounds-1.htm
http://www.myspace-help.com/Myspace-Comments-Spring-1.htm

or even better which is how i wanted it the first time
http://www.myspace-help.com/Abstract_Backgrounds-1.htm
http://www.myspace-help.com/Spring-1.htm

And then my View.php Sting


http://www.myspace-help.com/Myspace-Backgrounds/view.php?cat=Animated-Backgrounds&layout=animated_7.gif
http://www.myspace-help.com/Myspace-Comments/view.php?cat=Autos&layout=auto1.gif

To Anything like

http://www.myspace-help.com/Animated-Backgrounds-animated_7.gif
http://www.myspace-help.com/Autos-auto1.gif

Any Help Would be Appricated
If you really want to help Please msg me on msn or Aim
Sherman Destroy
<snip/>
I Would be forever Greatful

Also i am reading threw all of yours tips now thanks for replys and going to apply what you wrote to what i have

Ok So i got them all shortened but this is the whole problem i have so far.

Michael says: (2:48:22 PM)
RewriteRule ^(.)/(.).htm$ $1/index.php?page=$2 [L]
RewriteRule ^(.)/(.)/(.).htm$ $1/2$/index.php?page=$3[L]
RewriteRule ^(.
)-(.)-(.).htm$ $1/$2/$3.htm [L] [NC]
RewriteRule ^(.)_(.).htm$ $1/$2.htm [L]
RewriteRule ^(.)-(.)-(.).gif$ $1/view.php?cat=$2&layout=$3.gif [L] [NC]
RewriteRule ^(.
)-(.)-(.).jpg$ $1/view.php?cat=$2&layout=$3.jpg [L] [NC]
RewriteRule ^(.)-(.)-(.*).png$
This rewrite creates these pages

http://www.myspace-help.com/Myspace-Backgrounds-Animated_Backgrounds-animal_13.gif

What i want is

http://www.myspace-help.com/Myspace-Backgrounds/?page=1
to
http://www.myspace-help.com/Myspace-Backgrounds-1.htm

http://www.myspace-help.com/Myspace-Backgrounds/Abstract-Backgrounds/?page=1 to
http://www.myspace-help.com/Abstract-Backgrounds-1.htm

http://www.myspace-help.com/Myspace-Backgrounds/Abstract-Backgrounds/view.php?cat=Abstract-Backgrounds&layout=Abstract-1.jpg
to
http://www.myspace-help.com/Abstract-Backgrounds-Abstract-1.htm

Ive collabed with my server owner and other forums and read for days and tested everything and i keep messing it up and getting 500’s or not the result im looking for

tim,

[rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/rant #1]

For example, your first RewriteRule says to take http://example.com/.htm and send it to http://example.com//index.php?page={null}. Of course, the succeeding RewriteRules only make your “acceptable” URLs even more ridiculous. PPPPPPP (Proper Prior Planning Prevents Piss Poor Performance)!

So much for the lashing with the wet noodle! :whip: Where to from here? On the assumption that you’re using a Latin character set ([a-zA-Z]), USE IT to replace your :kaioken: EVERYTHING :kaioken: atoms! Moreover, use the correct metacharacter:

? = zero of one

  • = zero or more
  • = one or more

Rarely will you want zero or more!

THEN, ORDER is everything! If you’d started your RewriteRule sets with RewriteRUle ^(.) script.php [L] then you’d NEVER have the chance to match any of your RewriteRules ending in .htm, .jpg, .gif, .etc. Why? Because (.) has already matched it and redirected to script.php (which will also match and generate a LOOP!). The general rule with respect to using “lazy regex” is to use the most specific first then “graduate” to the “mass grab” of “garbage.”

Finally, you SHOULD get a 500 error from the mod_rewrite parser because you didn’t complete the .png’s RewriteRule!

[COLOR="Red"]# DO NOT USE (without significant modification and repair)![/COLOR]

RewriteRule ^[COLOR="Red"](.*)[/COLOR]/[COLOR="Red"](.*)[/COLOR].htm$ $1/index.php?page=$2 [L] 
RewriteRule ^[COLOR="Red"](.*)[/COLOR]/[COLOR="Red"](.*)[/COLOR]/[COLOR="Red"](.*)[/COLOR].htm$ $1/2$/index.php?page=$3[L]
RewriteRule ^[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR].htm$ $1/$2/$3.htm [L] [NC] 
RewriteRule ^[COLOR="Red"](.*)[/COLOR]_[COLOR="Red"](.*)[/COLOR].htm$ $1/$2.htm [L] 
RewriteRule ^[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR].gif$ $1/view.php?cat=$2&layout=$3.gif [L] [NC] 
RewriteRule ^[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR].jpg$ $1/view.php?cat=$2&layout=$3.jpg [L] [NC] 
RewriteRule ^[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR]-[COLOR="Red"](.*)[/COLOR].png$ [COLOR="Red"]{???}[/COLOR]

May I respectfully recommend a thorough reading of the mod_rewrite tutorial linked in my signature. It’s helped MANY members thus far and should give you the basic understanding of both regex and how it’s employed to make mod_rewrite such a powerful tool.

Regards,

DK

tim,

Apologies - I was answering this last night before disaster struck (ADSL modem problems).

If I remember correctly, I first commented on your code then ranted about something else:

[COLOR="Blue"]RewriteEngine on[/COLOR]
# WWW Only
RewriteCond &#37;{HTTP_HOST} !^www\\.Myspace-Help\\.com$ [NC]
RewriteRule ^(.*)$ http://www.Myspace-Help.com/$1 [R=301,L]
# I would have used
# RewriteRule .? http://www.Myspace-Help.com%{REQUEST_URI} [R=301,L]
# but your version is fine (Apache 2.x)

# http://www.myspace-help.com/Backgrounds/[COLOR="Red"]?page=1[/COLOR] to Myspace-help.com/Myspace-Backgrounds-1.htm
# You need a RewriteCond to access page={value}
RewriteRule ^([a-zA-[COLOR="Red"]Z-_[/COLOR]]+)/([a-zA-[COLOR="Red"]Z-_[/COLOR]]+)-(\\d+)\\.htm$ $1/$2/?page=$3 [L,NC] 
# Z-_ is which range of characters?RewriteRule ^([a-zA-Z-_]+)/(\\d+)\\.htm$ $1/?page=$2 [L,NC]
# Ditto AND No Case in a RewriteRule :nono:
RewriteRule ^([a-zA-Z-_]+)_(\\d+)\\.htm$ $1/$2.htm [L,NC] 
# Double ditto
# Catagory Rewrite
RewriteRule ^([A-Za-z0-[COLOR="Red"]9-_[/COLOR]]+)-(\\d+)\\.htm$ Myspace-Backgrounds/$1-$2.htm [L,NC]
# same with 9-_ what range is that?
# View.php Rewrite

Okay, you did order by most specific first and you’re rid of the (.*) but, because the - is a metacharacter within range definitions, it MUST be either the first (preferred) character or last!

Also, what’s ?page=1 got to do with those RewriteRules?

I’m rushing off to a meeting but will check back later.

Regards,

DK

that’s why he should always separate the page id from the name of the title. Title-12.html
myspace-backgrounds-12.html it’ll be better if he did myspace_background-12.html

Thanks, Rémon! You saved thatismeeee from rant #1!

BTW, I also agree with Shaydez’s comment on the - vs _. Personal preference, though.

Regards,

DK

What i am understand from ur problem is::: Setting Search Engine Friendly URL’s to your site…
For this online tool to help you regarding this:::
http://www.webconfs.com/url-rewriting-tool.php
Follow this, it will help you…

Okay, so you want to rewrite Myspace-Backgrounds1.htm to Myspace-backgrounds/?=page1

The problem here is that you use the dreaded .* which will match “Myspace-Backgrounds1” as a whole, whereas you’d want to match “Myspace-Backgrounds” and “1” separately. This means you also need two atoms – one to match the name of the directory and the other one to match the page number.

To match the page name you can use the simple atom ([a-zA-Z-]+) --which matches all characters from a through z lowercase and uppercase, and hyphens, multiple times-- and the page number can be matched using (\d+) --which matches all digits from 0 through 9, multiple times–

Put those together and you get:


RewriteRule ^([a-zA-Z-]+)(\\d+)\\.htm$ /$1/?=page$2

Note that this will not work when a directory ends in one or more digits, for example if you have /myspace-101/?=page12, the new URL will be /myspace-10112.html and apache has no idea where the directory name stops and the page number starts – using my code above the directory will be “myspace-” (because I didn’t allow digits in the directory name, exactly for this reason) and the page will be “10112”, which of course won’t work

That being said, as long as you stay clear from using digits in the directory names the code above should work :slight_smile:

PS. If you are on apache 1.x you should change ^ to ^/ and (\d+) to ([0-9]+)