Jump to content
Larry Ullman's Book Forums

Mod_Rewrite Not Working Correctly

Recommended Posts

I'm trying to do mod_rewrite like this:




RewriteEngine On

RewriteRule ^cancer/?$ cancer.php

RewriteRule ^ptsd/?$ptsd.php




I have my testing directory, named minus, so the same code should be at

adviceofthequeen.com and adviceofthequeen.com/minus/


I put this code into the .htaccess file in minus directory and when I type into browser:


the cancer.php page comes up correctly.


But when I type into browser:


the cancer.php pages comes up but without the style sheet and images.


So I also put this same code into .htaccess file above the minus directory (the real live code) and when I type into browser:


it throws a 500 Internal Server Error.


I'm pretty sure that if I ask hosting tech support about this they will suggest I use the cpanel redirect. I want to do this for all my files and now there is only 6 but there could be maybe about 50 when I'm done. It doesn't seem that redirect is the best way to do this.


Is there any syntax error in my mod_rewrite?


There was nothing in the .htaccess files except directory protection in the minus directory which is temporarily not on right now.


Apache version 2.2.21

PHP version 5.2.17

MySQL version 5.0.92-community-log

Link to comment
Share on other sites

There is a syntax error in your .htaccess: you're missing a space on the last line, before the destination.


Also, the reason your CSS and images don't show up with cancer/ is that the browser thinks its in a subdirectory and you're probably using relative paths to the CSS and images.

Link to comment
Share on other sites

Thank-you for finding the syntax typo.

Evidently, that is the reason it wouldn't run in my live directory!


(I've changed the names of my pages to add _treatment at the end.)


I took off the / from the cancer_treatment page and that works well now.

The only time it would show 404 not found is if the user typed it into browser with the /.

My code won't use that.

Does that make sense to you that I should do it that way or do you think I have to use absolute referencing?


This is the code I'm using now:



RewriteEngine On

RewriteRule ^cancer_treatment?$ cancer_treatment.php

RewriteRule ^ptsd_treatment?$ ptsd_treatment.php

Edited by abigail
Link to comment
Share on other sites

Here's something I thought of now. Can I write just one RewriteRule that will change all my file names, here are examples:


RewriteRule ^cancer_treatment$ cancer_treatment.php

RewriteRule ^hiv_aids_treatment$ hiv_aids_treatment.php

RewriteRule ^sids_treatment$ sids_treatment.php

RewriteRule ^sleep_disorders_treatment$ sleep_disorders_treatment.php

RewriteRule ^asthma_treatment$ asthma_treatment.php


So if it ends with _treatment then I want .php added.


The reason I'm doing this is for SEO. That's why I want treatment in all the names.

Link to comment
Share on other sites

You can definitely do this as one rule. My inclination is to be specific:

RewriteRule ^(cancer_treatment|hiv_aids_treatment|sids_treatment)/?$ $1.php


But you could also write something more flexible, if you'd rather. I would prepare for the / and use absolute references to my CSS and images.

Link to comment
Share on other sites

Actually, I figured out an even better way for me. So my customer can type into browser just cancer and my program can use cancer_treatment. They can even type in just hiv or just aids or hiv_aids. Here's what my code looks like then:



RewriteEngine On

RewriteRule ^cancer[a-z_]*$ cancer_treatment.php

RewriteRule ^hiv|aids[a-z_]*$ hiv_aids_treatment.php

RewriteRule ^sleep[a-z_]*$ sleep_disorders_treatment.php

RewriteRule ^sickle[a-z_]*$ sickle_cell_anemia_treatment.php

RewriteRule ^addict[a-z_]*$ addiction_treatment.php

RewriteRule ^fear|phobia[a-z_]*$ fear_phobia_treatment.php



Regarding my previous post, the reason to use one Rule is because I wouldn't have to edit file each time I add a new treatment. So there is not much advantage to do it your way, I would think. But I like my new idea much better anyway.


I suppose I will have to use absolute referencing for the css and images. Otherwise user sees 404 error but that is only if they type in the trailing /. Someone might do that I suppose.


Anyway, except for that minor problem of trailing / everything seems to be working great now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...