Forum Moderators: phranque

Message Too Old, No Replies

Help with RewriteRule please (driving me crazy)

         

impfut

1:48 pm on May 14, 2010 (gmt 0)

10+ Year Member



Hi everyone. I've been trying to sort this for over 2 hours (I must be very dumb!).

My website has the URL's set up as:
http://www.mydomain.com/?page=contact

For this I am using the following htaccess file:

Options +FollowSymLinks
RewriteEngine on
# RewriteRule
RewriteRule (.*)/$ /?page=$1


It works perfectly and allows me to reference the URL as:
http://www.mydomain.com/contact/


HOWEVER (the bit that I can't sort)...

Sometimes I need to return a feedback message so the URL changes to:
http://www.mydomain.com/?page=contact&feedback=1

I want my htaccess file to rewrite this as:
http://www.mydomain.com/contact/fb=1


Can any kind and clever person help me to rewrite both URL's so that they work with and without the feedback on the end? Sorry if I'm a stupid newbie (which I am).

Thanks for any help.
Dave

impfut

4:22 pm on May 14, 2010 (gmt 0)

10+ Year Member



Was really hoping someone could help. I've been torturing myself with page refresh for the last 4 hrs. Anyone?

g1smd

4:45 pm on May 14, 2010 (gmt 0)

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



You can't have the equals sign in the path part of the URL so that rules out using that particular URL.

Additionally, you don't need the trailing slash on extensionless URLs. In that case you can change the URL to
http://www.example.com/contact
and the pattern from
^(.*)/$
to
^([^/.]+)$
or similar.

It then becomes easy to make your new URL
www.example.com/contact/1
and test for that using the pattern
^([^/.]+)/([^/.]+)$
or
^([^/.]+)/([0-9])$
(if it is always a single digit) or
^([^/.]+)/1$
(if it is always a "1") or similar.

A trailing slash indicates a folder, so I would avoid mixing your URL format for pages with that pre-existing convention.

impfut

5:39 pm on May 14, 2010 (gmt 0)

10+ Year Member



Awesome! I got it working following your advice. THANKS!


Options +FollowSymLinks
RewriteEngine on
# RewriteRule
RewriteRule ^([^/.]+)$ /?page=$1
RewriteRule ^([^/.]+)/([^/.]+)$ /?page=$1&feedback=$2


So now my URL's are http://www.example.com/about and http://www.example.com/about/2

Would you advise adding something like .html on the end or is this perfectly fine and won't create any more browser processing etc..?

Thanks again
Dave

[edited by: impfut at 5:52 pm (utc) on May 14, 2010]

g1smd

5:39 pm on May 14, 2010 (gmt 0)

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



No. Don't add .html to the URLs. Omitting this makes the patterns much easier to build, as you look for something that does not contain a period.

Do add the [L] flag to every rule.

Please edit your post and use "example.com" so that the forum does not reformat your URL examples.

Do make sure that your script returns an immediate HTTP 404 header if I ask for URLs like
example.com/wrongword
or
example.com/contact/9
etc.

As for timing, this isn't a free help desk. You need to take into account, both time zones and people actually doing real paid work and getting to the forum when those commitments are completed. :)

impfut

5:51 pm on May 14, 2010 (gmt 0)

10+ Year Member



I noticed the domain link issue but couldn't spot the edit post button (*since found*).

I know it's not a free help desk. Thanks for your free help.

Dave.

g1smd

6:00 pm on May 14, 2010 (gmt 0)

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



Now you need to build a set of rules for when there is a direct client request for a URL like
[b]ex[/b]ample.com[b]/?[/b]page=contact&feedback=1
or
[b]www.[/b]example.com/[b]index.php[/b]?page=contact&feedback=1
and all other similar combinations.

This rule will need to test THE_REQUEST in a RewriteCond so that it only fires for requests from browsers and searchengines and not as a result of a previous internal rewrite. There are thousands of prior code examples in the forum.

It will redirect the user to the new, correct, URL so that the browser can request the new URL. This also helps searchengines to fix their listings.

This redirect should be listed before your general non-www to www canonicalisation redirect.

All of the redirects must be listed before any of the rewrites.

impfut

7:09 pm on May 14, 2010 (gmt 0)

10+ Year Member



Ok, I've managed to improve it (I think) based on what you've said. This is all very alien to me though and I'm really struggling to know hoe to perfectly redirect everything.

For now I'm redirecting ://exa.. to ://www.exa.. and if .com/index.php appears on its own. It gets redirected to .com/

I think anything further is beyond me for now (slow going). I will also sometimes have a url such as http://www.example.com/contact/1?action=yes&hello=world&etc=etc and I'm fine with those extra vars staying as they are (temporary for a php get whilst logged in.


Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# RewriteCond1
RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

# RewriteCond2
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# RewriteRule
RewriteRule ^([^/.]+)$ /?page=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)$ /?page=$1&feedback=$2 [L]

g1smd

7:56 pm on May 14, 2010 (gmt 0)

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



The first rule can be expanded to look at the parameter values in THE_REQUEST and then redirect to the new exact correct page URL.

However, this needs a lot more thought now you have changed the goalposts by explaining that actually there can be other parameters in the URL.

impfut

9:10 pm on May 14, 2010 (gmt 0)

10+ Year Member



Too much thought lol. Appreciate your help though chap.

Cheers
Dave