Using "P" flag in .htaccess

software development

#1

Hey Folks,

I need to accomplish something using .htaccess that I haven’t done before. I believe it should be accomplished using the “P” flag, and Dreamhost tech support tells me mod_proxy is indeed installed so that should be available.

I just created an example that should make my problem very clear.

When you view www.bhaktibreakfastclub.com/example1.html it successfully shows you www.bhaktibreakfastclub.com/example.php?n=1.

This was done using this line in my .htaccess file:
RewriteRule example1.html http://www.bhaktibreakfastclub.com/example.php?n=1 [NC]

So far so good. However, I don’t actually want viewers to see the long version in their navigation bar. I’d like the short .html URL shown in the navigation bar, with the .php?n=… version displayed in the browser. I believe this is what the P flag is meant to accomplish.

And so a second example with the P flag: When you visit www.bhaktibreakfastclub.com/example2.html it fails to show you www.bhaktibreakfastclub.com/example.php?n=2.

I had put into my .htaccess file:
RewriteRule example2.html http://www.bhaktibreakfastclub.com/example.php?n=2 [P,NC]

Same as example 1, just added the P flag.
So, why does example 2 fail?

Thanks anybody for your help!
~Daniel


#2

What you’re trying to do does not require the proxy flag. The first example you gave can be implemented as:

Having the domain name in there was forcing this rewrite to generate a browser redirect. Without it, the rewrite can take place internally.


#3

I think mod_proxy is only available on PS and Dedi and it’s not the thing to use when writing “pretty URLs”.

If your post was a bit more than just an example and you actually do have a huge list of exactString# rewrites you can save some typing with this kind of thing:

RewriteEngine On RewriteRule ^example(.*)\.html$ /example.php?n=$1 [NC]

Google mod_rewrite or pretty urls.


#4

You guys rock, thanks so much.

Removing the domain name part of the URL was the key thing. Now, with

RewriteRule example1.html /example.php?n=1 [NC]

it works beautifully. Pretty URL indeed!

Thanks again,
Daniel


#5

Since you are using PHP anyway, why not just write a dispatcher into your app? Just redirect every request to a single file, such as index.php, and use PHP to parse your requests. It may be easier than htaccess.

If you want to go with the htaccess route, you know you don’t need to create a rule for every page, right? You can use patterns such as:

Above is untested, but will probably work.


#6

Update: Played around with patterns, per bobocat. Now I’m using:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^example([0-9]+)$ /example.php?n=$1 [NC]