Forum Moderators: phranque
I'm new to forum and fairly new to .htaccess and php stuff. Here is my problem. For my gaming website, I need...
http://www.example.com/game.php?id=3 to be re-written to
http://www.example.com/game/3/50-State-Capitals.html
And I need the .htaccess file to re-write every game.php?id={#} to
http://www.example.com/game/{#}/{game-name}.html
My .htacces file reads as follows:
Options -Indexes
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/?([^/]*\index\.php?¦[^\./]*)[:;,\.]*$ /games/index.php?act=Arcade&do=newscore [L,NS]
#RewriteRule ^/?([^/]*\.php?¦[^\./]*)[:;,\.]*$ /games/index.php?act=Arcade&do=newscore [L,NS]
#RewriteRule ^game/([_A-Za-z0-9-]+)/([_A-Za-z0-9-]+)/?$ game.php?id=$1&temp=$2
RewriteRule ^game/([^/.]*)/?/([_A-Za-z0-9-]+).html/?$ game.php?id=$1&temp=$2
RewriteRule ^games/([_A-Za-z0-9-]+)/?$ viewcat.php?c=$1
I don't know how to fix it. Any help would be appreciated.
Eric
[edited by: jdMorgan at 9:52 pm (utc) on Oct. 30, 2008]
[edit reason] No URLs, please. Use example.com. Please see Terms of Service and Forum Charter. [/edit]
I suspect you are going about this backwards -- It's a common problem here.
If you are seeking to use "SEO friendly" URLs, then you need to do the following:
You have inquired about only the final --and optional-- step.
Here [webmasterworld.com] is a thread describing the process, in detail.
Information about mod_rewrite, regular expressions, and how to get the most out of this forum is available in our Apache Forum Charter [webmasterworld.com].
Jim
Anways, I think I understand what you mean. I read your article and post, and what I am getting from it is...
1.) I need to change all links to friendly links. (I have done this).
2.) Have .htaccess write the user friendly link to the dynamic php link so it knows where to get the page from. (Have not done).
3.) Send the dynamic php page back to the friendly html (which I have sort of done).
Am I on the right path with this?
An external redirect is an HTTP response to the client, requesting it to ask again for the resource it requested using a new URL. This response from the server terminates the current HTTP transaction, and it is up to the client to begin a new transaction using the URL supplied by the server in its redirect response.
An internal rewrite simply takes the incoming URL and translates it to a new filepath -- That is, a filepath that is different in some way from the result of the default server URL-to-filepath translation. This rewriting is done within the context of the current HTTP transaction, and is not visible to the client.
Another problem with the word "send" is that a redirect is not a command, it is a request to the client (browser or robot) to ask again for the resource it requested using a new/different URL. It is the client's choice whether it wants to do so. Remember the analogy: client=customer, server=waiter -- The client is in charge of the transaction (He can leave the restaurant if the service is poor). Clients will follow redirects 99.999% of the time, but it is nevertheless useful to remember who is in charge... :)
Do not try to do Step 3 until you have finished coding and testing Step 2; Step 3 is an optional and final step, and can interfere with getting the other steps working. Plus, there is a 'trick' to it, as outlined in the thread I cited: You must check the client request to be sure that the current URL-path is "unfriendly" because the client requested it, and not because your other rule has just rewritten it.
Since mod_rewrite is recursive in an .htaccess context, this check involving using RewriteCond to check %{THE_REQUEST} is required in order to prevent an 'infinite' rewrite-redirect loop (also called a dead-loop) caused by the rewrite rule (Step 2) and your redirect rule (Step 3) countermanding each other.
Jim
The way you do this depends on how PHP is installed on your server, but you'll be using either AddType or AddHandler (see Apache mod_mime). Our PHP forum has more info on this, both searchable and in the PHP forum library.
If you do decide to change your URLs and take the hit in search engine rankings while your site is being re-indexed and your TrustRank is being re-established, then don't change from .html to .php... Instead, go with entensionless URLs, and rewrite those to .html or .php files as appropriate.
I suggest searches on "parse html as (or for) PHP" and "extensionless URLs" to get a lot more info on these subjects. See the WebmasterWorld "Search" link in the link list at top left/center of almost all pages.
Jim
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^game/([0-9]+)/([_A-Za-z0-9-]+)\.html$ game.php?id=$1&temp=$2
Now, on my site, my links read ...com/game/{#}/{numbers,letters,and dashes}.html
My understanding is that my .htaccess file will keep example.com/{#}/{anything}.html in the URL, find example.com/game.php?id={#}, and load the example.com/game.php?id={#} page, but keep the clean .html URL for the viewer.
Is this thinking correct?
Thanks again.
Eric
The only thing I'd add is an [L] flag on the end; Basically, for efficiency's sake, you should always use [L] on every rule, until you have a very good reason not to.
When the server receives a request for the URL-path "/game/123/4a_56-78.html", it will change that to an internal request for the filepath "/game.php?id=123&temp=4a_56-78" and then invoke its content-handler. In other words, it will 'call' or 'run' game.php and pass those two parameters to it as "GET variables".
Jim
[edited by: jdMorgan at 7:50 pm (utc) on Oct. 31, 2008]
Also, should the .htaccess file be placed in my root directory (which I have read it should be) or in my game folder in my root directory?
Sorry if that was wordy or unclear. I am running off to soccer practice right now.
Thanks for the help,
Eric
[edited by: EricEC at 8:32 pm (utc) on Oct. 31, 2008]
Your actual game pages are now "/game/123/4a_56-78.html" or similar. The "page" is defined by its URL, which is defined by links on your pages. It just so happens that you are now rewriting requests for you pages to a file (your script), which generates those pages. But the "page" is its URL, and the filename does not matter as far as the Web is concerned.
So don't confuse the filepath with the URL; They are no longer the same thing. Just like mod_rewrite, we will need to know what both the URL and the filepath are in order to answer that question.
Jim
[edited by: jdMorgan at 9:53 pm (utc) on Oct. 31, 2008]
I have figured out my problem [Finally :)]
My original .htaccess code:
Options Indexes
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/?([^/]*\index\.php?¦[^\./]*)[:;,\.]*$ /games/index.php?act=Arcade&do=newscore [L,NS]
#RewriteRule ^/?([^/]*\.php?¦[^\./]*)[:;,\.]*$ /games/index.php?act=Arcade&do=newscore [L,NS]
#RewriteRule ^game/([_A-Za-z0-9-]+)/([_A-Za-z0-9-]+)/?$ game.php?id=$1&temp=$2
RewriteRule ^game/([0-9]+)/([_A-Za-z0-9-]+)\.html/?$ game.php?id=$1&temp=$2
RewriteRule ^games/([_A-Za-z0-9-]+)/?$ viewcat.php?c=$1
All I did was add FollowSymLinks after Indexes and it worked (I found this in another one of your posts). I'm all good to go now, no problems, I am just curious as to why that little code was needed.
Thanks for everything.
Eric
You can remove the "/?" from the beginning of the patterns in your first two rules if this code is intended for use ion .htaccess. While you'll see that construct used in code that is intended for use in either server config of .htaccess files, it actually creates a duplicate-content problem, and is not generally a good idea.
Jim