Forum Moderators: phranque

Message Too Old, No Replies

Simple Mod Rewrite Trouble

Mod Rewrite

         

solokron

10:09 am on Oct 8, 2006 (gmt 0)

10+ Year Member



I thought this would be simple but I am having issues figuring out how do to this properly with mod rewrite. Here is what I am trying to do. I want to rewrite the url (not redirect). These files exist within a subdirectory.

/product1.php
/product2.php

to...

/product1/
/product2/

etc.

solokron

10:10 am on Oct 8, 2006 (gmt 0)

10+ Year Member



I should mention these files have different names as well.

equalm

2:02 am on Oct 9, 2006 (gmt 0)

10+ Year Member




seems like you should use the pass through option [PT], rather than the redirect [R=permanent] or [R]

RewriteRule ^/([a-z0-9])+\.php /$1/index.php [PT]

-George

solokron

8:21 am on Oct 9, 2006 (gmt 0)

10+ Year Member



Wow thank you George! Unfortunately it does not appear to work. I have verified Rewrite is functioning.

jdMorgan

1:17 pm on Oct 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Which URL-paths designate files that actually exist?

/product1.php
/product2.php

or...

/product1/
/product2/

I suspect it's the first group, in which case, the code above is 'backwards' -- We usually want to rewrite from URLs that don't exist (or that we wish to replace) to URL-paths that do exist (or invoke scripts).

The [PT] flag has a specific function not explicitly needed here; It switches mod_rewrite from its default URL-to-filename mode to URL-to-URL mode, so that mod_rewrite's output can be processed by other URL-to-filename translation modules that execute subsequently.

Jim

equalm

5:14 pm on Oct 9, 2006 (gmt 0)

10+ Year Member




Hmmm, I probably don't get what you're asking for exactly.,

/product1.php should effectively land the person on /product1/product1.php? or /product1/ by itself?

If you want to change the URL that's in the browser, you're going to have to do a redirect. If you want the URL to look like /product1.php, but actually be /product1/index.php (or whatever you define in the second arg of the Rewrite rule) then I believe my rule works.

I did a simple test on my apache instance and it seems to do this

/product1.php > /product1/index.html (but browser keeps /product1.php)

using this rule

RewriteRule ^/([a-z0-9]+)\.php /$1/index.html [PT]

PT is not what you use if you want to change what the URL is in the browser though. If you want to do that, there's no way getting around the redirect option.

equalm

5:15 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



oops. i also screwed up my original post, which had

([a-z0-9])+

it should have been

([0-9a-z]+)

oops.

equalm

5:16 pm on Oct 9, 2006 (gmt 0)

10+ Year Member




darn. i keep confusing people.

basically the "+" should be inside the parentheses, not outside. 0-9a-z can be in any order.

equalm

5:22 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



To add on what Jim wrote, since this is a hack rewrite and you don't want to subject it to all the other rewrite rules you might have in place, you might consider putting [PT,L] in this rewrite rule to make it the last one worked on and bypass all the other rewriterules you have going.

A useful thing to turn on for debugging (but it adds logs like crazy) is the

RewriteLog /wherever/you/want/rewrite.log
RewriteLevel 5

hope that helps

solokron

7:26 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



I see. What I have is a new link directory script that is outputting files of the category name, Other-Providers.php for example. My previous output the categories into directories each with their own index.php, index2.php etc. /Other-Providers/index.php for example.

As Google PR is an important factor for many link partners I want the structure to appear as the previous to maintain PRs as the new files obviously have a PR of 0.

I have tried rewriting the script a few times but it is very messy once I make changes as the script has a very large feature set.

I want the browser to show

/Other-Providers/
when loading Other-Providers.php

solokron

7:28 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



I should note that I have though of simply creating the directories and creating index.php files with includes for their associated files. The problem with this is the category index and features still point to the files which make that option less prudent.

equalm

7:35 pm on Oct 9, 2006 (gmt 0)

10+ Year Member




i see. you want the incumbent, already-existing directory URL to effectively be the /new-directory.php file.

If the naming is consistent, then we can probably get away with a reverse of our above rule

RewriteRule ^/([a-z0-9]+)/.* /$1.php [PT,L]

be warned, though, this rule is going to pass through ANY url that has /blahblahblah/ to effectively be /blahblahblah.php

If you have a small # of directories to do this for (doubtful), you might want to add a rewriterule for each one. kludgy, but it will be a finite rule. In general, any rewriterule is a dangerous one since it is going to affect every single incoming request, and we can't always replicate the situation for the traffic coming in.

Good luck.

solokron

7:56 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



Great thanks! Some of the admin files are in the same directory as the links so I would want to create individual entries for each. Is it also possible to write a ruleset that would only affect that directory? That would lower processing.

equalm

11:03 pm on Oct 9, 2006 (gmt 0)

10+ Year Member




You would set up a variety of conditions before processing. Saves a little processing per request

RewriteCond ^/thefolderyouwanttocheck/
RewriteRule

Whatever Rewriterule follows the RewriteCond you set is the one that will be triggered, should the RewriteCond be valid

[httpd.apache.org...]

solokron

11:08 pm on Oct 9, 2006 (gmt 0)

10+ Year Member



Thank you George and Jim! Much appreciated!

jdMorgan

1:25 pm on Oct 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But note in that same documentation that RewriteConds are not processed unless the RewriteRule matches. This is due to legacy reasons, but also allows a RewriteCond to back-reference a RewriteRule pattern, while still allowing a RewriteRule to back-reference a RewriteCond value.

Therefore, in order to maximize performance, it is the RewriteRule pattern which should be most selective, so as to avoid processing the RewriteConds except when necessary.

Jim

equalm

5:45 pm on Oct 10, 2006 (gmt 0)

10+ Year Member




I defer to Jim on this. My experience only goes so far...and I think we've reached the end of the road. =)

solokron

2:06 am on Oct 19, 2006 (gmt 0)

10+ Year Member



Hmmm. Just when I thought I had it.

I have created an .htaccess file in the directory I want affected with the following:

RewriteRule ^/([a-z0-9]+)/.* /$1.php [PT,L]

It does not appear to function though. Do I need to throw this into the httpd.conf file?

solokron

2:13 am on Oct 19, 2006 (gmt 0)

10+ Year Member



So this is what I am looking to do which should reduce the load a bit.

Browser Address Bar Show -> /resources/Other-Providers/
Contents loaded from -> /resources/Other-Providers.php

jdMorgan

3:06 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must remove the leading slash from the URL-path if you want to use the code in .htaccess. Also, the trailing ".*" on the pattern is superfluous, and only wastes CPU time:

RewriteRule ^([a-z0-9]+)/ /$1.php [PT,L]

Jim

solokron

4:41 am on Oct 19, 2006 (gmt 0)

10+ Year Member



I created a .htaccess file in /resources/:
Included:

RewriteRule ^Affiliate-Programs/ /$Affiliate-Programs.php [PT,L]

loaded up domain.com/resources/Affiliate-Programs/
and it resulted in a 404 error.

jdMorgan

5:09 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd expect it to generate a 404, since that code is invalid.

Your code should look like this:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-z0-9]+)/ /$1.php [PT,L]

Depending on your server configuration, you may not need the Options directive, but if you do need it, then mod_rewrite won't run without it. On the other hand, you may not be allowed to use the Options directive, in which case it will cause an error.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Also, whenever you get an error, look at both your server error log and your server access log. They will often give information that will allow you to quickly determine the problem.

Jim

solokron

5:13 am on Oct 19, 2006 (gmt 0)

10+ Year Member



Yes, but I want to specify filenames as listed previously.

solokron

8:07 pm on Oct 19, 2006 (gmt 0)

10+ Year Member



" So this is what I am looking to do which should reduce the load a bit.

Browser Address Bar Show -> /resources/Other-Providers/
Contents loaded from -> /resources/Other-Providers.php "