homepage Welcome to WebmasterWorld Guest from 54.211.201.65
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod ReWrite - Help Required
How to use Mod Rewrite to make long urls short
domingo




msg:1495986
 3:11 pm on Oct 28, 2003 (gmt 0)
Hi,

I have been tryign to solve this with the help of Webmaser World and other forums but somehow could not get any success. May be some experience user can help me on this.

I have some urls at a site with the format

http://www.mydomain.com/results.php?term=search_term

Here are my questions

1.) How can I make the URLS appear like this in the user browsers

http://www.mydomain.com/search_term.html

2.) Do I have to change the links manually on my site from http://www.mydomain.com/results.php3?term=search_term to http://www.mydomain.com/search_term.html OR will it be done automatically.

Looking forward for your help. In the meanwhile I will keep digging and trying to solve it myself :)

 

jdMorgan




msg:1495987
 4:41 pm on Oct 28, 2003 (gmt 0)

domingo,

2.) Do I have to change the links manually on my site from http://www.mydomain.com/results.php3?term=search_term to http://www.mydomain.com/search_term.html OR will it be done automatically.

Yes, anything that feeds URLs to the browser should be changed to specify the new short URL, and then let mod_rewrite translate that short URL back into the long one if it is required for input by your script.

Jim

domingo




msg:1495988
 4:53 pm on Oct 28, 2003 (gmt 0)

Thanks for the replies. I am looking at site point article right now.

JD any thoughts on question No. 1

Longhaired Genius




msg:1495989
 5:17 pm on Oct 28, 2003 (gmt 0)

This is the first thing I would try but I haven't tested it ("Options +MultiViews" means you don't have to add .html to your search term)

-----------.htaccess file------------

Options +FollowSymlinks +MultiViews

RewriteEngine On
RewriteRule (.*) results.php?term=$1 [L]

domingo




msg:1495990
 9:49 am on Oct 29, 2003 (gmt 0)
Hi,

Tried everything, but so far no luck. Now my .htaccess looks like this

RewriteEngine on
RewriteBase /mod
RewriteRule ^(.*)mod/(.*)\.html$ http://www.mydomain.com/results.php3?term=$2 [R=301,L]

I am testing it in a subdirectory so that I dont mess up with my main site.

Please correct me if I am wronge. In my opinion the above should convert any URLS even non existing to the later one.

Example

If someone tries for
http://www.mydomain.com/mod/search.html

It should be converted to
http://www.mydomain.com/results.php3?term=search

Instead of doing this the URL it is picking up is my 404 page.

Thanks for helping me out.

domingo




msg:1495991
 6:46 pm on Oct 29, 2003 (gmt 0)
Yahooooooooo!

Finally successful, the final code looks like this

RewriteEngine on
RewriteBase /mod
RewriteRule (.*).html$ http://www.mydomain.com/results.php3?term=$1 [R=301,L]

Now when I try to access http://www.mydomain.com/mod/anything.html it automatically transfers me to http://www.mydomain.com/results.php3?term=anything

Now coming back to the small hitch

The URL in the browser also displays as http://www.mydomain.com/results.php3?term=anything

Is there any way that the URL remains the same as http://www.mydomain.com/mod/anything.html

jdMorgan




msg:1495992
 6:53 pm on Oct 29, 2003 (gmt 0)
domingo,
[code]
RewriteRule (.*)\.html$ /results.php3?term=$1 [L]
[/code]
See:
http://httpd.apache.org/docs/mod/mod_rewrite.html
http://httpd.apache.org/docs/misc/rewriteguide.html

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved