Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod ReWrite - Help Required

How to use Mod Rewrite to make long urls short

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

10+ Year Member


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 :)

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

JD any thoughts on question No. 1

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

10+ Year Member



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]

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

10+ Year Member


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.

6:46 pm on Oct 29, 2003 (gmt 0)

10+ Year Member


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

6:53 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member


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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month