Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite not cooperating

         

Todd

3:51 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Hey all,

I'm working on a website and I'm trying to make it more SEO friendly. So I'm using mod_rewrite to do this.

I'm trying to turn this URL: blablabla.com/products.php?id=tester -> into -> blablabla.com/products/tester/

but I am having no luck. I tried several tutorials and said-to-be proven examples that work all with failure. My site just returns a 404 and doesn't accept my RewriteRules. For some reason, it thinks I'm trying to access a sub-directory and ignores my ".htaccess" file.

Here's the code I was trying to use for the example above:

RewriteEngine on
Option +FollowSymLinks
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [L]

Any idea on what's going on or what's wrong with it? If someone could help me get something working, I'd greatly appreciate it.

Thanks,
Todd

g1smd

4:08 pm on Jun 3, 2009 (gmt 0)

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



This question came up at least a dozen times in the last month in some form or another. It's one of the more popular topics. :-)

Have you changed the links on your pages to use the new format URLs?

The Rewrite does not 'make' anything. It merely allows requests for friendly URLs to pull content from file location inside your server where the content really resides.

You also need to decide whether a valid URL request has a trailing slash or not. To allow both to trigger a rewrite is a Duplicate Content issue. Pick one, and redirect the other one with a redirect ahead of the rewrite.

 RewriteRule ^products/([^/]+[b])$[/b] [b]/p[/b]roducts.php?id=$1 [L] 

If you're not careful, the result of your rewrite could be something that matches your pattern and will be rewritten again, in an infinite loop. In that case, you will need a RewriteCond checking THE_REQUEST so the rewrite is invoked only for direct client requests. There's many previous threads with example code for that; several in the last week.

[edited by: g1smd at 4:15 pm (utc) on June 3, 2009]

[edited by: jdMorgan at 7:26 pm (utc) on June 3, 2009]
[edit reason] Closed [ code ] tag. [/edit]

Todd

4:14 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



I know it doesn't make things but for some reason mod_rewrite doesn't like it when I put forward-slashes in a rule. I can get it to accept anything as long as it doesn't have a forward-slash in the REGEX pattern. The server misconstrues it as physically accessing a directory.

What I'm wondering is if there's a condition or some option I can specify to the mod_rewrite engine to fix it.

It's like telling mod_rewrite to handle requests to /foo/bar. "foo" exists but "bar" is a rewrite rule. Apache thinks I'm accessing "foo/bar" and since it knows I'm not accessing the "foo" directory directly, it ignores my ".htaccess" file even though "bar" does not exist. That's my problem. :-(

g1smd

4:18 pm on Jun 3, 2009 (gmt 0)

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



That's because a URL with a trailing slash does mean you want a physical folder on the server. In that case you're better to go extensionless.

Is this rule in httpd.conf or in .htaccess? If it is in .htaccess, is that file in the root or in a folder?

Todd

4:19 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Sorry for double posting, but I'll skim through some of the old posts.

Is there a way to specify a REGEX pattern to make the trailing slash optional? Would it be something like: (/$¦$)

g1smd

4:22 pm on Jun 3, 2009 (gmt 0)

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



You don't want two different URLs to be able to trigger the same rewrite.

You need to avoid making Duplicate Content in that way.

Redirect to your chosen format first, then once the user is requesting the correct URL, and only then, do the rewrite.

Todd

4:24 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



It's an .htaccess file and it's in a subdirectory. I put it in a folder called "cms" and am testing it out before I go live with it. So it's not in the root directory. I tried doing that too but for some reason it doesn't make a difference (at least with my .htaccess file).

jdMorgan

7:29 pm on Jun 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a rule in the root directory's .htaccess file which would capture and rewrite this request before your subdirectory /cms/.htaccess code has a chance to see it?

Jim

Todd

8:33 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



No. There's only the .htaccess file in the "cms" directory.

I was looking through some of the older posts in this forum but they don't help. :-(

Is there some directive or way to "spoof" Apache to ignore forward-slashes (indicating directories but NOT the trailing slash) and "emulate" it through a rewrite rule?

I was playing around with it and it works fine but whenever I try to write a rule for handling a request with a forward-slash (like a sub-directory) the server ignores the rewrite rules and thinks I'm accessing a real directory or real file, despite it being a rewrite rule in .htaccess.

Would there be some line or directive I'd have to put in the .htaccess file to tell Apache to ignore forward-slashes as sub-directories?

jdMorgan

8:39 pm on Jun 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing wrong with your code, it's something else interfering.

If you are on Apache 2.x, disable AcceptPathInfo
On any version of Apache, disable the MultiViews Option
(Assuming in both cases that you don't need these features.)

Jim

Todd

9:10 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



It looks like the Options -MultiViews parameter works! The server is configured very strangely and unfortunately I don't have root access. :-P The web host configures PHP as a CGI and not as an Apache mod and still uses version 1.3.29.

Well I think that's what I needed! Thanks for all your help!

Also, is there a guide to all the .htaccess file directives and parameters? I know Apache has theirs but is there a more condensed version by a 3rd party?

g1smd

9:44 pm on Jun 3, 2009 (gmt 0)

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



The Apache site is the most correct, but one of the most unreadable (simply due to the concise nature of the writings).

There are other sites, but I've found accuracy (especially in example code) lacking in many of them.

Todd

9:59 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



So this is a case where the creators know best. I'll read up on their docs despite their length.

Well thanks for all your help guys!