Forum Moderators: phranque

Message Too Old, No Replies

Newbie help needed with redirect to expanded url.

url redirect, .htaccess

         

4mguy

6:46 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Hi,

My realty script assigns each property listing an ID; E.g. 44-10

I want a .htaccess to redirect urls like this:

[mydomain.com...]

to

[mydomain.com...]

Reason being that the system does not accept the first url as a valid url. All the info that I have read on the subject is good for truncating urls, but not for expanding them. The '44-10' could be any value, however, it would have to be carried over to the end of the expanded url.

Thanks for helping.

Guy

mark_roach

7:09 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Try something like this:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*) /search_id.php?ID=$1

gergoe

7:24 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



This is one of the basic rewriting techniques, you should be able to find several examples about this topic, although the usage varies, it might turn out to be difficult to find the right one.

If your url really looks like you mentioned, then the following will do the trick:

RewriteEngine on 
Options +FollowSymLinks
RewriteRule ^(\d+-\d+)$ /search_id.php?ID=$1

This rule rewrites addresses like /3233-43, /1-1 and /1312312-0 to the one you need, but for example will not rewrite the following: /23423-a, /323-123.html or /23232-

4mguy

10:52 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



Thanks guys. I really appreciate your help.

Here's the results:

The first suggestion above from Mark_roach produces a 500 Internal Server error and the second from Gergoe produces a page not found.

And yes, I am using the format shown above. The only difference is that I am using a .us domain and not a .com. E.g.

[mydomain.us...]

must become

[mydomain.us...]

44-11 is derived from a property listing's ID, which will change with each listing.

Suggestions?

Guy

gergoe

11:31 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



RewriteEngine on 
Options +FollowSymLinks
RewriteBase /
RewriteRule ^(\d+-\d+)/?$ search_id.php?ID=$1 [L]

Removed the trailing slash from filename, added an optional trailing slash (so if the browser decides to add it, it will still work), added the [L]ast modifier, so if there are more rules, it will not continue to parse them and added RewriteBase which helps in situations when the url and the path of the filesystem does not strictly correlate.

4mguy

3:55 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Hi Gergoe,

Tried your code and no luck?

Also tried

Redirect /44-10 [mydomain.us...]

and it works. However, I can't get it to work for any values for 44-10. So I am stuck. Is there a way of using the above format so that the '44-10' variable is copied to its ID=44-10 position?

Thanks for helping.

Guy

jdMorgan

4:07 am on Dec 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use a Redirect, then your 'internal' script URL will be "exposed" to clients, and it will be listed in search results, thus negating any advantage of trying to use 'search-friendly' URLs.

If you are getting a 404 error with the mod_rewrite code gergoe posted above, then examine your error log, paying close attention to the filepath that fails; It is possible you may need to set RewriteBase to something other than "/" for use in your server environment.

You may also wish to try a 'backwards-compatible' way of writing the RewriteRule pattern for older server and OS versions:


RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteRule ^([0-9]+-[0-9]+)/?$ search_id.php?ID=$1 [L]

Here "[0-9]+" replaces "\d+" -- The two are equivalent ways of saying "match one or more digits," but support varies across server and OS versions.

However, it is more likely that the RewriteBase needs to be adjusted.

[added] Also, be sure to completely flush your browser cache after making any changes to your .htaccess code. [/added]

Jim

[edited by: jdMorgan at 4:08 am (utc) on Dec. 11, 2007]

4mguy

5:18 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Your code works! Thank you to all for your help - really appreciate it.

Small technical hitch; entering the url www.mydomain.us/44-10 works, but www.mydomain.us/44-10/ produces the result, but with half the page missing. How can I make sure the trailing slash is ignored in the url?

Guy

gergoe

5:58 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Without rewriting:
URL: http://www.example.com/search_id.php?ID=44-10
Image src: images/spacer.gif
Resolved to: http://www.example.com/images/spacer.gif

With rewriting:
URL: http://www.example.com/44-10/
Image src: images/spacer.gif
Resolved to: http://www.example.com/44-10/images/spacer.gif

The path of the image is resolved by the browser, not by the Apache, so there's no way of ignoring it, you either convert your paths to absolute paths (so images/spacer.gif becomes /images/spacer.gif), or you make an another rewriting rule to fix urls like /44-10/images/spacer.gif.

I suggest you to check this recent thread [webmasterworld.com] with a similar problem, in that thread both options are explained (look for message 3522922).