Forum Moderators: phranque
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
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-
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
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.
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
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]
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]
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).