BetaONE will rise again!

BetaONE will rise again! (http:\\b1.hcanet.com\forum/index.php)
-   Coders Corner (http:\\b1.hcanet.com\forum/forumdisplay.php?f=57)
-   -   Need Help With URL Manipulations. (http:\\b1.hcanet.com\forum/showthread.php?t=15306)

SlickVic78 6th Jan 05 12:52 AM

Need Help With URL Manipulations.
 
Hello All,

I decided to try and play around with directives/parameters you can place within .htaccess within a directory on a web hosting server. I am looking to do the following and really have no clue (after looking through the vast amount of sites online) what to really use (since I am not sure what I am looking for).

This is what I would like to accomplish:

Change This: http://www.mydomain.com/index.php?pg=home
To This: http://www.mydomain.com/home

Obviously, the modified URL will act just like the original link so that I can easily use the pg variable within my code (since I have a few checks on the variable "pg").

Also, continuing with this same concept, would it be easy to pass multiple variables within the same URL (example: http://www.mydomain.com/index.php?pg=home&cmd=do)?

Any help would greatly be appreciated.

Thanks in advance!:)
-SlickVic78

SlickVic78 13th Jan 05 01:30 AM

I ended up finding out how to do what I wanted to do with this URL manipulation. Instead of doing the folder structure, I decided to go the file structure route. Both can easily be accomplished writing a few lines to the .htaccess of the folder you want to apply this to.

So far things are working very well.

Woot! :)

-SlickVic78

war59312 14th Jan 05 02:15 AM

Can you post the code please. ;)

SlickVic78 14th Jan 05 02:50 AM

Hello war59312,

Sure thing. All it requires is a few lines within your .htaccess file and you are good to go.

Suppose you have a URL that looks like this:
To manipulate your URLs, simply add this code to your .htaccess file:
Code:

RewriteEngine On
RewriteRule ^catalog-?([^-]+)-([^-]+)(.*)\.html$ /catalog$3.html?$1=$2 [QSA,N]
RewriteRule ^catalog-?\.html$ /catalog.php [QSA,L]

The first rule loops until no name-value pair is available and the second rule calls the catalog.php if no name-value pair is available.

So you would use this URL instead:
[EDIT] Removed ".php" after catalog. [/EDIT]

You would then have to be sure that all your URLs on your page reflect the new URL format, and you should then be good to go.

If you would prefer the Folder Structure like:
Let me know and I'll be more than happy to share my findings.

-SlickVic78

war59312 14th Jan 05 03:12 AM

Yea that would be great too. ;)

Thanks a ton,
Will

PS: Bet this helps with google too. ;)

SlickVic78 14th Jan 05 04:21 AM

Quote:

Originally Posted by war59312
Yea that would be great too. ;)

Thanks a ton,
Will

PS: Bet this helps with google too. ;)

Hey Will,

It should help with all search engines. Google started indexing variables within the URL, but to play it on the safe side, it wouldn't hurt to use this method.

Please take note of the edit above. I had to correct the URL you would use if you were to apply the code in my last post.

Here is the code to place within you .htaccess if you want the folder structure. Remember though - since the URL is being re-written, your images may break because of the folder structure. I will comment on that later. Here is the code you would place in the .htaccess if you were to want folder structure URL manipulation:
Code:

RewriteEngine On
RewriteRule ^catalog/([^/]+)/([^/]+)(.*)$ /catalog$3?$1=$2 [QSA,N]
RewriteRule ^catalog/?$ /catalog.php [QSA,L]

The URL to use for this would be (using above example URL):
Now, the way to fix the image breakage, there are 2 ways:
  1. Have all your images with full paths (ex- http://www.mydomain.com/images/image.jpg)
  2. Add more lines to the .htaccess
Let's say you had an image directory (src="images/image.jpg") and a scripts directory (src="scripts/script.js") that needed to be adjusted with how many folders you are using in the URL - you would then use this:
Code:

RewriteRule ^.+(images|scripts)/(.*)$ /$1/$2 [L]
This would bring you back to the doc root for your images and scripts directory. This way it will stay consistant with no images or scripts breaking do to the changing of the URL structure.

If you do not modify this, you will end up with this as an example:
Where it should be:
Also, if you have more than 9 variable pairs (&something=something) within your URL and are running apache >1.3.28 or >2.0.45, you may want to add this line within your .htaccess:
Code:

RewriteOptions MaxRedirects=25
This is to increase the maximum number of redirects from the default value 10 to 25 (these are so called internal redirects). This is an apache prevention for endless loops.

All this I learned from Robert over at http://forum.modrewrite.com/. This guy was totally helpful and has gotten back to me with every question I threw at him.

I hope this helps you out Will as much as it did for me! :)

Enjoy!
-SlickVic78

war59312 14th Jan 05 05:03 AM

Heck yea, thanks a ton. :)


All times are GMT +1. The time now is 04:35 AM.

Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.