Forum Moderators: phranque
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/
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 :/
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.
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
# 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]
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
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
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!
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.
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]
Thankyou.
[edited by: jdMorgan at 2:30 pm (utc) on Dec. 2, 2009]
[edit reason] example.com [/edit]
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
As such it is 'just as safe' as submitting a form with those two optional values missing.
Jim