View Full Version : mod_rewrite hedaches
solarnexus
04-26-2006, 03:43 PM
I'll keep this short, mainly because Apache's mod_rewrite is driving me up the wall:
I have a webpage. A php webpage. Links will be like so:
mysite/link1
which should redirect to ./index.php?p=link1. I currently have the following .htaccess file:
RewriteEngine On
RewriteRule /* /index.php?p=$1
The main thing is I _need_ this to work at home, and at bluehost... so any help is apreciated.
vegasgwm
04-29-2006, 06:29 PM
you will need to setup/configure your system at home like bluehost setup, or just make 2 seperate .htaccess files one 4 home , one 4 online.
May I suggest, do a search for mod_rewrite, there are a lot of posts on this topic. (btw i have a feeling your re-write is not entirely correct)
Zonteck
04-30-2006, 06:33 AM
Yeah, your best bet would be to do a search for mod_rewrite related info.
But a quick tip.
You need to correct your links in your files so they link to the new rewrite'd format (ie, change <a> links from index.php?p=whatever to /p/whatever) and a working verison of your rewrite rule would be:
RewriteRule ^p/(.+)/? index.php?p=$1
atomicnuke
08-21-2006, 05:30 PM
I'd like some help with mod_rewrite too. The url will be set as mysite.com/poems/title. If they go to mysite.com/poems then it'll just load the index page of the poems folder. But if they go to say: mysite.com/poems/dark-day then it will load mysite.com/poems/poem.php?name=dark-day How would I go about that?
redsox9
08-21-2006, 05:40 PM
I found this site a few months back ... this may answer questions for everyone. :cool:
http://www.ilovejackdaniels.com/apache/mod_rewrite-cheat-sheet/
atomicnuke
08-21-2006, 05:49 PM
I saw that site and tried:
RewriteRule ^/poems/?$ /poems/poem.php?name=$1 [L]
But couldn't seem to get it working, the title will just contain letters, numbers and hyphens, I just get a file not found error.
redsox9
08-21-2006, 05:59 PM
I saw that site and tried:
RewriteRule ^/poems/?$ /poems/poem.php?name=$1 [L]
But couldn't seem to get it working, the title will just contain letters, numbers and hyphens, I just get a file not found error.
I believe you need to have something like this...
RewriteRule ^/poems/([A-Za-z0-9-]+)/?$ /poems/poem.php?name=$1 [L]
Does that work? Otherwise your code seems valid... it's similar to what I have at my site.
atomicnuke
08-21-2006, 06:06 PM
Nope, that doesn't work either. The site is an add-on domain, so it is a subfolder to the public_html, but the .htaccess file I'm editing is the one inside the subdomain folder. I'll keep tinkering and trying to figure it out.
-edit-
I got it working I just took out the first / after the start string symbol, is it because I had RewriteBase / in there too?
Rotsky
08-22-2006, 01:19 AM
I got it working I just took out the first / after the start string symbol, is it because I had RewriteBase / in there too?
Yep. You might also want to add a rule before the current one to add a trailing slash for those people who don't add it themselves. I'd also place some limits on the number of characters in the names, so that you still generate a 404 for URLs that are wrong. Eg, if you decide that the names after 'poems/' will always be at least five chars and a max of 20 chars, I'd write the rule thus:
RewriteRule ^poems/([A-Za-z0-9_-]{5,20})$ poems/$1/ [R]
RewriteRule ^poems/([A-Za-z0-9_-]{5,20})/$ poems/poem.php?name=$1 [R]
redsox9
08-22-2006, 04:49 AM
I got it working I just took out the first / after the start string symbol, is it because I had RewriteBase / in there too?
I thought I had removed and noted that when I cut and pasted your original code but, yes, the first forward slash should have been deep-sixed there. I'm glad to hear that you have it working now!
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.