Forum Moderators: phranque

Message Too Old, No Replies

Browser request rewrite rule issue

         

zorro

1:20 pm on May 17, 2010 (gmt 0)

10+ Year Member



With reference to jdmorgan's post #:4123704

I am having issues getting it to work correctly!

My htaccess code:

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ www.example.com [L,R=301]
#(ABOVE IS FOR NON WWW TO WWW)

#BROWSER REQUEST IS FOR OLD URL http://www.example.com/accommodation.php?id=89
#CODE FROM POST #4123704:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(accommodation\.php)?\?id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(accommodation\.php)?$ http://www.example.com/%2 [R=301,L]

RewriteRule ^([0-9]+)$ accommodation.php?id=$1 [L]

USING 89 AS AN EXAMPLE!

When I enter http://www.example.com/accommodation.php?id=89 into the browser it returns the correct page but says the following in the browser address bar:

http://www.example.com/89?id=89

Instead of:

http://www.example.com/89

Help greatly appreciated!

g1smd

1:44 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add a question mark to the end of the target URL to clear the query string.

zorro

2:41 pm on May 17, 2010 (gmt 0)

10+ Year Member



BRILLIANT!

THANKS g1

zorro

6:26 pm on May 17, 2010 (gmt 0)

10+ Year Member



I seem to have come up against another problem with regard to the rewrite rule I posted above.

Here it is again:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(accommodation\.php)?\?id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(accommodation\.php)?$ http://www.example.com/%2? [R=301,L]

RewriteRule ^([0-9]+)$ accommodation.php?id=$1 [L]

The above works fine and produces the desired result:
http://www.example.com/89 instead of the old result http://www.example.com/accommodation.php?id=89
and broswers redirct work fine too!

But I missed a major part out and was looking at an old htaccess file on my pc and not the correct one on my server.

NEW REWRITE RULE:
The actual rewrite rule should have been:
RewriteRule ^([A-Z][a-z]+)/([0-9]+)$ accommodation.php?accommodation_type$1&id=$2 [L]

As I am naming the accommodation type from the accommodation type in the database so the following result shows:
http://www.example.com/Apartment/89
or
http://www.example.com/Villa/89
or
Bungalow, Studio, Townhouse etc etc, whatever the accommodation_type is.

The php code is like this...<a href='".$row[accommodation_type]."/".$row["id"]."'> for links within the site.

How would I adjust the 'RewriteCond' and 'RewriteRule' above for the 301 browser redirect to take account of this new RewriteRule?

Many thanks in advance!

jdMorgan

2:36 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> How would I adjust the 'RewriteCond' and 'RewriteRule' above for the 301 browser redirect to take account of this new RewriteRule?

You would adjust them by modifying the regular-expressions patterns to match the actual requested URLs, after referring to a good regular-expressions tutorial such as the one cited in our Forum Charter [webmasterworld.com]. Please try this yourself before posting here, as requested by our Charter.

Thanks,
Jim

zorro

1:28 pm on May 19, 2010 (gmt 0)

10+ Year Member



Thanks for the advice Jim but after having read the regular expression documentation (3 times) plus other stuff recommended, I am still none the wiser!
I am not a coder and this sort of stuff just frazzles my brain unless there is a very similar example shown to what I am looking for.
I think i'll just give up as a bad job and stick with the dynamic url's instead of SEO friendly ones as I am spending too much time on the issue, or at least have them like this instead of trying to get them to look like they are in folders...

http://www.example.com/Apartment-89
instead of
http://www.example.com/Apartment/89

or

http://www.example.com/Florida-Apartment-89
instead of
http://www.example.com/Florida/Apartment/89

Thanks again to all.

jdMorgan

4:55 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I recommend that you do not use a trailing slash on "documents" or "pages" -- Trailing slashes are for directories.

They cause two problems: First, search engines may "look for other files" inside your pseudo-directories, and second, adding a "directory-level" to your URLs will break any page-relative links and included-object references (e.g. image, css, and JS file includes) on your pages, requiring you to modify all those links and references.

Re: "Not a coder"... It is time to become a coder then, because it is needed to reliably control your server configuration, which is what a .htaccess file does. These files comprise the infrastructure of your site, and if that infrastructure is not solid, your site may never achieve its full potential.

Regular expressions is a concise language for matching character-patterns -- no more, no less. Further, regular-expressions form the basis of many very-useful directives and functions in almost all modern scripting and programming languages -- PHP, PERL, JavaScript, Python, C, and many, many others. Many *nix command lines and some code editors can use them as well.

A few hours spent with the regex tutorial cited in our Apache Forum Charter, and using it to "take apart" the patterns you find in the rules you see posted here will be time very well-spent. All that remains is for you to decide to do it, and we here will support that effort.

The two basic concepts of regex are "match the input character string to this pattern" and "remember the part of the input string that matches this part of the pattern, so we can use it later."

In your case, the "opposite" of the internal rewriterule

RewriteRule ^([A-Z][a-z\-]+)/([0-9]+)$ accommodation.php?accommodation_type$1&id=$2 [L]

would be this external redirect rule

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(accommodation\.php)?\?accommodation_type=([A-Z][a-z\-]+)&id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(accommodation\.php)?$ http://www.example.com/%2/%3? [R=301,L]

So there's an example for you to analyze... :)

To allow hyphens in your "accommodation-types," you need to match them in both of your rules, so that's been added here.

I should also add that writing regular expressions is usually much, much easier that reading them. This is because when you write them, you know what goal you are seeking at that time. When you go back to read the pattern later, you may not remember the goal -- unless you include complete, concise comments in your code.

Here, the RewriteCond is looking at the HTTP Request line received from the client, which will be
GET /accommodation.php?accommodation_type=Florida-apartment&id=89 HTTP/1.1
or it could be
GET /?accommodation_type=Bungalow&id=12 HTTP/1.1

Now I hope you'll take my counsel, and figure *how* these two new examples work. Be assured that we *will* be able to tell the next time you ask a question here... :)

Jim

zorro

5:20 pm on May 20, 2010 (gmt 0)

10+ Year Member



No doubt you will be able tell from future questions (ha!)

Thanks for all your help JD, I will try and digest your comment.
I do spend hours and hours trying different connotations before asking for help here at webmasterworld and I am learning lots, thanks again.

jdMorgan

5:58 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, use that regex tutorial cited in our Forum Charter -- I still often do... :)

Jim