Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Mod Rewrite

Help editing .htaccess file.

         

EricEC

7:48 pm on Oct 30, 2008 (gmt 0)

10+ Year Member



Hello all willing to help,

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]

jdMorgan

10:02 pm on Oct 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Eric, and Welcome to WebmasterWorld!

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:

  1. Change all of the links on your pages to the new friendly format. These are the URLs that search engines will see and use.
  2. Internally rewrite SEO-friendly URL requests arriving at your server to the "unfriendly" form needed to call your script to generate the 'next page'.
  3. (Optional) Redirect direct client requests for the 'unfriendly' URLs to the new friendly URLs to speed up search engine indexing of these new URLs.

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

EricEC

12:57 am on Oct 31, 2008 (gmt 0)

10+ Year Member



Thanks, and sorry for not following forum guidelines in my first post.

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?

jdMorgan

1:34 am on Oct 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but there's no "sending" involved -- There is internal rewriting and external redirecting. Those phrases are quite precise, so use them to avoid confusion.

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

EricEC

3:10 am on Oct 31, 2008 (gmt 0)

10+ Year Member



Okay, thank you for the help.

I'm going to try to get my html URLs to be viewed as php URLs. Hopefully this is what I should be doing.

Thanks again for helping me. I'll be back if I encounter any more issues.

Eric

jdMorgan

2:51 pm on Oct 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't change the URLs, just set Apache to parse .html files for PHP includes.

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

EricEC

5:39 pm on Oct 31, 2008 (gmt 0)

10+ Year Member



Okay, here is what I have...

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

jdMorgan

7:50 pm on Oct 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, and that code is *very* close to perfect, so good job!

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]

EricEC

8:18 pm on Oct 31, 2008 (gmt 0)

10+ Year Member



Okay, great. I'll be working on this so if I have any problems I'll be back :)

Thanks again for all of the help.

Eric

EricEC

8:31 pm on Oct 31, 2008 (gmt 0)

10+ Year Member



Sorry, quick question. It doesn't seem to be working as of yet (still working on figuring out what is wrong). The &temp=$2 part. You said that calls what would be in my case the game name, however, my actual game pages are www.example.com/game.php?id=123. So do I need to get rid of the &temp=$2 part of add something to my php code?

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]

jdMorgan

9:41 pm on Oct 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> however, my actual game pages are www.example.com/game.php?id=123. So do I need to get rid of the &temp=$2 part.

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]

EricEC

12:00 am on Nov 1, 2008 (gmt 0)

10+ Year Member



Back again :)

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

jdMorgan

12:23 am on Nov 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Options +FollowSymLinks or Options +SymLinksIfOwnerMatch is required in order to enable mod_rewrite, as stated in the mod_rewrite documentation. On many servers, it is already set in the main server configuration, and need not be set in .htaccess. The result when it is not set is a 500-Server Error, which I would have expected you to report if you'd seen one...

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