mod_rewrite question

They have: 4 posts

Joined: May 2005

Hi,

I want to know if this is possible.

There's a section of my website where I use a select box to reach a slew of other pages.

<option>page1</option>
<option>page2</option>
...
'

What I want to happen is, when the form is submitted, instead of
"http://mysite.com/dir1/index.html?page=page1"
I want it to look like
"http://mysite.com/dir1/page1/"

Is that actually possible?

I've tried this code out, but it doesn't seem to work

RewriteCond %{QUERY_STRING} ^page=([A-Za-z]*)$
RewriteRule ^/$ http://%{SERVER_NAME}/dir1/%1/? [R]
RewriteRule ^([A-Za-z]+)/?$ /dir1/index.html?page=$1 [L]
'

Actually, heres my entire rewrite rules

RewriteEngine On
RewriteBase /dir1/

RewriteCond %{QUERY_STRING} ^page=([A-Za-z]*)$
RewriteRule ^/$ http://%{SERVER_NAME}/dir1/%1/? [R]

RewriteRule ^([A-Za-z]+)/?$ /dir1/index.html?page=$1 [L]
'

Thanks in advance!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

another option you could use (and I have used it before) is to set your 404 error for the dir1 directory to point to the index.php file. then in the index.php check the URL being called, and treat it the same as you do with page=

If I remember right, you also need to output a header to overide the default 404 header the server would give out.

-Greg

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Ouch, I've thought of doing this myself a few times but surely you'd be breaking a real 404 error, also it seems a little like a 'dirty hack'?

Could you then write some code so that a genuine 404 would be recognised as such and generate an error instead of dumping the user back to the index page?

a Padded Cell our articles site!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

yes on mine, as soon as it figures out what you are trying to access, it looks to see if that file exists. If it doesn't, it will then say that the "module" is 404error and send a 404 header.

Now we use something similar for our affiliate program, so that affilaites can jsut give out domainname.com/affiliateID instead of domainname.com/index.php?affiliate=affiliateID (I like simplified URLS for peopel to remember).

However, if someone can give the solution to doing this with a Rewrite condition, I would also prefer that! Smiling

-Greg

They have: 4 posts

Joined: May 2005

welp, I gave it my best and couldn't really come up with a solution.

So I did a partial implementation of your suggestion Greg. Instead of using a error page, I just used the same page. The first thing I do is check to see if the form has been submitted, if it has, I get the value selected from the jump box, then I create the "pretty" url and lastly, use "header" to recall the page, but using the pretty url. At that point when the page loads again, the working mod_rewrite rewrite rule takes over.

Hopefully someone will come along with a solution. I'd love to see how, if it can be done.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I know this tread is old, but in searching for something else, I came across this tutorial on how to do this without the error page or the mod_rewrite. So I looked this thread up to post the URL so I could easily find it later:

http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

-Greg

They have: 7 posts

Joined: Sep 2005

This is the "Wizard" I use for generating my mod_rewrite rules, they have a page option and directory option that will accomplish exactly what the original poster requested:

http://www.webmaster-toolkit.com/mod_rewrite-rewriterule-generator.shtml

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.