Forum Moderators: phranque

Message Too Old, No Replies

Help with htaccess, how do I do this?

htaccess woes

         

Ben878

7:47 pm on Nov 3, 2009 (gmt 0)

10+ Year Member



Okay so I can do fairly simple stuff in htaccess.

This is essentially my htaccess file:


RewriteEngine on
RewriteRule ^games/([0-9]+)$ /games/$1/ [R]
RewriteRule ^games/([0-9]+)/$ /games.php?show=$1
RewriteRule ^games$ /games/ [R]
RewriteRule ^games/$ /games.php


RewriteRule ^games/([a-z]+)$ /games/$1/ [R]
RewriteRule ^games/([a-z]+)/$ /games.php?genre=$1


RewriteRule ^games/([a-z]+)/([a-z]+)$ /games/$1/$2/ [R]
RewriteRule ^games/([a-z]+)/([a-z]+)/$ /games.php?genre=$1&sortby=$2


RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)$ /games/$1/$2/page$3/ [R]
RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)/$ /games.php?genre=$1&sortby=$2&pageno=$3

Now I have one small issue when a user submits a form I'd like to redirect them from
/games/?genre=variable&sortby=variable
To
/games/variable/variable/

I don't want the user to be able to access the url /games/?genre=variable&sortby=variable

Thanks

EDIT:

I came up with:

RewriteCond %{QUERY_STRING} genre=([a-z]+)
RewriteCond %{QUERY_STRING} sortedby=([a-z]+)
RewriteRule ^games\.php$ /games/%1?/%2?/ [R]

However all that does is rewrite the url funny:
/games/title/?/%253f/

Ben878

9:19 pm on Nov 3, 2009 (gmt 0)

10+ Year Member



Tried again, sorry for bump but it wouldn't let me edit it anymore.

Anyway here is my latest attempt:

RewriteCond %{QUERY_STRING} genre=([a-z]+)
RewriteCond %{QUERY_STRING} sortby=([a-z]+)
RewriteRule ^games\.php$ /games/%1/? [R]

Yes, this sortof works. However it only shows the sortby so the resultant url is:

/games/sortbyvariable/

Now this:

RewriteCond %{QUERY_STRING} genre=([a-z]+)
RewriteCond %{QUERY_STRING} sortby=([a-z]+)
RewriteRule ^games\.php$ /games/%1/%2? [R]

Was my next attempt yet it didn't work in the slightest. What am I doing wrong?

////////////////////////////////////////
EDIT:

It appears that I've correctly redirected it:

RewriteCond %{QUERY_STRING} genre=([a-z]+)&sortby=([a-z]+)
RewriteRule ^games\.php$ /games/%1/%2/? [R]

It does redirect. However, now I get a message from Firefox saying "This page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Removing the new redirect and then visiting games/genre/sortby/ works again :/

g1smd

11:58 pm on Nov 3, 2009 (gmt 0)

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



You have some infinite loops in there. You need a RewriteCond in front of the redirects, in order to detect it was a direct client request.

Your redirects are 302 redirects because you specified only [R], not [R=301].

Always include the canonical domain name in the target of any redirect.

List all redirects before all rewrites.

It's after midnight and I have been up since 6.30 am. Bed calls.

jdMorgan

1:29 am on Nov 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one example, using your most-complex rule-set. Your current rule-set of

RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)$ /games/$1/$2/page$3/ [R]
RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)/$ /games.php?genre=$1&sortby=$2&pageno=$3

should be re-coded as:

# Externally redirect (only) direct client requests for script filepath back to 'SEO friendly' URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /games.php\?genre=([a-z]+)&sortby=([a-z]+)&pageno=([0-9]+)\ HTTP/
RewriteRule ^games\.php$ http://www.example.com/games/%1/%2/page%3/? [R=301,L]
#
# Externally redirect requests w/missing trailing slash to add the slash
RewriteRule ^(games/[a-z]+/[a-z]+/page[0-9]+)$ http://www.example.com/$1/ [R=301,L]
#
# Internally rewrite 'SEO friendly' URL requests to games.php script filepath
RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)/$ /games.php?genre=$1&sortby=$2&pageno=$3 [L]

Jim

jdMorgan

1:42 am on Nov 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, at least one simple optimization appears to be possible. To add trailing slashes to all "games" URLs (if missing), only one rule is likely needed:

RewriteRule ^(games/[a-z]+(/[^/]+)*)$ http://www.example.com/$1/ [R=301,L]

Jim

Ben878

4:05 pm on Nov 4, 2009 (gmt 0)

10+ Year Member



Okay, I went away last night because there were no replies while I was working and I came up with:

RewriteEngine on
RewriteRule ^games/([0-9]+)$ /games/$1/ [R]
RewriteRule ^games/([0-9]+)/$ /games.php?show=$1
RewriteRule ^games$ /games/ [R]
RewriteRule ^games/$ /games.php

RewriteRule ^games/([a-z]+)$ /games/$1/ [R]
RewriteRule ^games/([a-z]+)/$ /games.php?genre=$1

RewriteRule ^games/([a-z]+)/([a-z]+)$ /games/$1/$2/ [R]
RewriteRule ^games/([a-z]+)/([a-z]+)/$ /games.php?genre=$1&sortby=$2

RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)$ /games/$1/$2/page$3/ [R]
RewriteRule ^games/([a-z]+)/([a-z]+)/page([0-9]+)/$ /games.php?genre=$1&sortby=$2&pageno=$3

RewriteCond %{THE_REQUEST} genre=([a-z]+)&sortby=([a-z]+)
RewriteRule ^games\.php$ /games/%1/%2/? [R]

////////////////////////////////////////
The above works without flaws. Now, if only someone could help me make it better and possibly point me in the right direction to learn htaccess better. All I seem to be able to find is things to copy and paste. No actual htaccess complete tutorials.

Thanks

jdMorgan

4:50 pm on Nov 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It has several flaws, one of which is 'critical' and the other of which is 'serious'.

I suggest that you analyze carefully the differences between this code and what I posted, as I don't have enough time here to repeat myself.

Critical: Your redirect status will be 302-Found and will result in major problems with the search engines, which will NOT update the old URLs if they get a 302 response.

Serious: The protocol and hostnames are not specified in your redirects, allowing duplicate content and redirection to a non-canonical domain.

You've also ignored the optimization I proposed, or chose not to comment on it.

Jim

Ben878

6:20 pm on Nov 4, 2009 (gmt 0)

10+ Year Member



I didn't ignore it, but thats what I came up with last night before I went to sleep. My only problem with yours is it is incredibly confusing. I really don't get htaccess but I like to be able to do things myself. It's not really productive for me to come back here everythime I need htaccess help.

I just tried your code and it worked. I'm not trying to be difficult and I'm sorry for any inconvenience.

Now I have one problem. This works:
[mysite.net...]

These don't:
[mysite.net...]
[mysite.net...]
[mysite.net...]

I'm going to give it an attempt based on what you gave me. However, please respond if I have not posted and edit of this post by the time you read it.

Thank you for everything!

g1smd

9:31 pm on Nov 4, 2009 (gmt 0)

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



Please do take the time to examine and understand the code in jd's post as it contains fixes for a number of common and serious errors.

TheMadScientist

9:52 pm on Nov 4, 2009 (gmt 0)

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



My only problem with yours is it is incredibly confusing. I really don't get htaccess but I like to be able to do things myself.

My personal advice is start in the Library (links below the bread crumbs) or hire a professional... The reason is a single, seemingly small mistake (as jdMorgan noted above) can not only break your site, it can ruin your Search Engine Rankings *, which unlike a broken site, can go unnoticed until a huge amount of damage has been done. (You'll need to understand regular expressions too. Mod_Rewrite is based on them and if you understand them what jdMorgam's rules do is not at all confusing... I would stop writing and start reading until you understand what you are doing.)

There are some really good 'basics' posts in the Library and they might help you find the answer to your most recent question. (The reason one works and the other doesn't is you're not matching the trailing / on the rule that redirects.)

* An example of this is on a site I used to run and recently took back over, someone changed a single rule, did not test (or forgot to empty their browser cache) and served the information from the wrong file for who knows how long before I came back... Nearly 500 pages were duplicated and 1000 were missing because of a single error. The error basically served the same directory twice, once in the wrong location, rather than serving two unique directories.

This is not a joke. Be very careful with your .htaccess and know + understand exactly what every thing in your file does... When I looked at the site, which should have been serving 'widget-info' and got 'wadget-plans' in the 'widget-info' directory all I could do was stare for a minute, because all the URLs were 'widget-info' and I was told no one had touched the .htaccess in months. It was a 'tiny' error, but it tanked the sites rankings and definitely gave visitors a bad experience.

Ben878

11:41 am on Dec 2, 2009 (gmt 0)

10+ Year Member



Right I've done quite a bit of work on my site since. Now I have one little problem right now:

Is this safe?


RewriteEngine on
# Externally redirect (only) direct client requests for script filepath back to 'SEO friendly' URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /games.php\?genre=([a-z]+)&sortby=([a-z]+)&dev=&title=\ HTTP/
RewriteRule ^games\.php$ http://example.com/games/%1/%2/? [R=301,L]
#
# Externally redirect requests w/missing trailing slash to add the slash
# Not needed as it is already executed earlier on RewriteRule ^(games/[a-z]+/[a-z]+)$ http://example.com/$1/ [R=301,L]
#
# Internally rewrite 'SEO friendly' URL requests to games.php script filepath
RewriteRule ^games/([a-z]+)/([a-z]+)/$ /games.php?genre=$1&sortby=$2&dev=&title=[L]

It is for when these variables "&dev=&title=" are set but have no values.
So.. Is this safe? If not what is the correct way to perform the same thing?

Thankyou.

[edited by: jdMorgan at 2:30 pm (utc) on Dec. 2, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:36 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what you mean by "not needed as it is already executed earlier on." Are you referring to code in another .htaccess file, or to code not shown here?

If not, then the rule certainly is needed, because the previous rule (shown here) only redirects requests for dynamic URLs, not requests for static/friendly URLs that are missing a trailing slash...

To your specific problem, some scripts require the parameter names in the query string, even if their value is blank. Some don't. So I can't answer whether your code is 'safe' in that respect, as I don't know what your script will do (or if it cares) if the name is present but the value is blank.

Or is that the problem you're asking about?

Jim

Ben878

6:06 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



The code is executed earlier on don't worry. Erm, it's the fact that when I submit a form there could be two blank fields because they aren't required. But GET add them to the end of the url with no values. So I want to know if it safe for me to redirect in the way I described using no values.

jdMorgan

7:07 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rule doesn't have anything to do with missing GET parameters, since it takes as input a client HTTP request for a static URL (for example "example.com/games/fps/price/" and maps that request to your script with two blank query parameters (example "/games.php?genre=fps&sortby=price&dev=&title=").

As such it is 'just as safe' as submitting a form with those two optional values missing.

Jim