Forum Moderators: phranque
I have been struggling with this Mod_Rewrite thing and with godaddy for a while now. But to no avail.
I am making this thread after reading and trying advice from this thread [webmasterworld.com]. After which I ended up even more puzzled than before... I thought that Godaddy simply had this feature as enabled by default as per their help texts, but they had some messup that prevented its proper usage. But then i try this and it redirected me no problems whatsoever.
Well... this is my full htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^index/readmore/([0-9]+)$ index/readmore/$1/ [R]
RewriteRule ^index/readmore/([0-9]+)/$ index.php?readmore=$1 I have read numerous tutorials about regex, and how to use it in the rewrite rules, i have seen some wizards that write the rules for you, and all those things that seem so good have done nothing for me... as my URL stays as it is... the url I intend to rewrite is my index.php as described on my second rule in the code block posted. My .htaccess file only contains that code and nothing more.
I think I have given all the details i can... other than this i really hope i dont get the dreaded 'change hosts' advice... i would hate to do that ... i am kinda lazy for that kinda thing right now lol.
Thanks in advance for at least reading this post and if you post an answer, thanks a lot too!
We need a few more: :)
There are a few more questions, but they concern tidying-up your code, and are not critical right now.
Jim
-Full url i would like to rewrite is as follows:
http://www.example.com/index.php?readmore=193
-Exact server filepath to my script!? if you mean to my .htaccess file, then it is in the root folder of my ftp. Same as my index.php file. its all in the same directory, so it 'should' work as far as I know of...
-How I test my code, well i followed the test to know if I should even bother with this from your advice (almost a year ago) in the thread I linked on my original post above. It redirected to google when I typed foo.html, so it should be working, but i reckon that maybe mod_rewrite is dual usage, like one thing is url rewriting and another thing is redirections... but I am no expert so I dunno how much true is this thinking of mine.
-Results on my browser... So, i go to my site, where there are a lot of links to pages like http://www.example.com/index.php?readmore=([0-9]+) and they are not re-written, i click on one of them, and they dont get re-written in my address bar either... so i think somethings wrong, (either that or i am expecting this to do something it does not do?). As far as error logs go, godaddy has a very strange way to deal with those, and aparently i had to do something before they even started collecting error data... so the logs were empty... dunno what i have to do to make em log my error data if any, but that is outside the scope of the thread here... so ill keep that to myself lol.
-How these results differ from my expectations is a simple question... it did not rewrite... i frankly had little expectation of this doing some url rewriting given the huge hype about godaddy being terrible with this subject, and not being able to even provide assistance makes them a crappy host... but i cannot move just yet... so i am giving the help seeking one more shot. ;)
I'll be happy to try and answer any further questions you may have. Thanks for your time in trying to help me out. :)
If it is your intent to use "search-engine-friendly URLs," then this is how it works:
Your second rule is essentially correct for step two, but you've missed step one.
RewriteRules are invoked when a request arrives at your server, and can be used to change (internally rewrite) the server filepath associated with the requested URL to a new, non-default filepath. This action takes place before any content is served or any scripts are invoked. RewriteRules *do not* change the links that appear on your Web pages.
RewriteRules can also be used to externally redirect one requested URL to another URL. This sends a response back to the client (browser or robot) and says, "That content has moved, please use this new URL, and send me another request for that content." That is what we do in step three, but that is not the critical function here.
In your old set-up:
http://www.example.com/index.php?readmore=193 was the (old) URL used in links, and
<DocumentRoot>/index.php?readmore=193 was the internal server filepath
In your new set-up:
http://www.example.com/index/readmore/193/ is the (new) URL used in links, and
<DocumentRoot>/index.php?readmore=193 is still the internal server filepath
So, you must internally rewrite from the URL http://www.example.com/index/readmore/193/ to the script filepath /index.php?readmore=193 -- which is exactly what your second rule does.
As stated above, an external redirect from the old URL http://www.example.com/index.php?readmore=193 to the new URL http://www.example.com/index/readmore/193/ is optional. We can discuss that after you get the first two steps working.
See this thread [webmasterworld.com] in our Apache Forum Library for a more-detailed description of this whole process.
Jim
(Apologies for the double posting)
EDIT:
I tried again, this time with an image instead of a php file, and it worked alright.
here is the rule i used as a test:
RewriteRule ^image/([a-z]+¦[A-Z]+)\.(gif¦jpg¦png)/$ /dead-sig.gif Then I went to my FF2 browser and typed, image/whatever.jpg
it took me to dead-sig.gif, did the same with whatever.png, and whatever.gif just to give it a shot... they all worked...
however, the rule I have set for index.php?readmore=### is not working, when I enter the static url i want to set, it takes me to a blank page with the No input file specified msg, and if I use the -MultiViews option then it takes me to a 404 error page...
Oh I am starting to think that I am too early to this dance... one has to study a lot to grasp the basics of this thing... it is soooo complicated... Oh well... no time like the present to start learning it then... I'll keep at it.
You could correct that and shorten it up a bit:
RewriteRule ^image/[a-zA-Z]+\.(gif¦jpg¦png)$ /dead-sig.gif [L]
And two more bits of advice: Always use the [L] flag on your rules unless you know why you don't want to, and stop right now and quit writing new rules until you get the rules above working. Do not complicate your test environment by adding more rules and their dependencies and interactions into the mix.
The error message you got sounds like it came from your script, or at least, from some script; It is not an Apache server error message. So, at least part of your rewrite-to-script rule is working. It sounds like the value of the "readmore" parameter is incorrect, or perhaps the script requires a second name/value pair. Try directly requesting (typing in) example.com/index.php or example.com/index.php?readmore= and see if you get the same error message that you're getting from your rewritten example.com/index/readmore/193/ URL request.
Did you get anything in your server error log? Your error log can be quite useful -or even critical- in these cases.
Please always provide example URLs and filepaths when discussing rewrites. Sometimes the devil is in the details, and with mod_rewrite, the devil is almost always in the details...
Jim
I have no idea how to bypass this message. But apparently the $_GET values are getting wiped out... however, if I hit your troubleshooting approach, and I attempt to use the original URL and send readmore= {nothing} then, i get redirected to my main page, instead of seeing an error message.
As for the image rewrite rule it was merely a test... i had to see something in action or I would flip crazy here... nothing seemed to work other than redirections... but it worked anyways... now on to this query string issue now.... grrr this is a tough one...
I am talking with godaddy to enable the error logs... they were disabled by default... (*curses*) so it will take a little while for them to get activated and start logging stuff... After that happens, my nxt post will include some data from the logs and hopefully I will be able to help you help me... lol XD Thanks for all your words of wisdom man!
EDIT:
There is nothing on my error log related to this... it was empty, so I tried entering a non-existent file request to trigger a log entry, it entered, but when this error pops up it doesnt trigger an error log entry. I will look into access log. Maybe it happens like that guy in the forums i read... it registered as accessed, but in reality it showed this msg and stopped there...
EDIT2:Nothing showed on my access log. So the index.php?readmore=193 is not getting hit with this method. (?)
RewriteRule ^index/readmore/([0-9]+)\.htm$ index.php?readmore=$1 @Jim:
I am using FF2 (its clear private data for FF2), and even after doing what you adviced on IE7, still the same results.
I managed to make it work.
What happened was that I had:
Option +FollowSimLinks So I removed the "+" symbol after reading [an article by Tom Gidden about .htaccess and mod_rewrite on GoDaddy] and it worked, but the site looked awful due to no css and img proper linking. So now I proceed to fix the src's and/or href's for all imgs, and/or links so they work properly with the new static url.
The .htaccess file I ended up with is as follows:
Options FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index/readmore/([0-9]+)\.htm$ index.php?readmore=$1 I am very grateful for all your help regarding this issue. It was vital for me in order to get this to work. It fueled my ideas and jump started me into looking anywhere for answers...
Now i can change the ugly dynamic urls and make them more seo friendly. Thanks for all the help! :D
[edited by: jdMorgan at 5:04 pm (utc) on Aug. 15, 2008]
[edit reason] No URLs, please. [/edit]
After fixing your images, look into adding rules to redirect the old dynamic URLs to your new static ones and to canonicalize your hostname (domain) by redirecting all non-canonical requests to your preferred domain, e.g. "www" or "non-www." There are many threads here on those subjects.
Jim
Options +FollowSymLinks -MultiViews -Indexes
Jim
Now, if I wanted to do this again, for a file in a subdirectory?
I want to try my hand at doing this on the forums. The URL to rewrite is: http://www.example.com/forums/viewthread/php?forum_id=2&thread_id=25&pid=9#post_9
My conceived RewriteRule is as follows:
RewriteRule ^viewthread/([0-9]+)-([0-9]+)-([0-9]+)\#post_([0-9]+)$ forum/viewthread.php?forum_id=$1&thread_id=$2&pid=$3#post_$4 My only doubt with this rule lies on the pound symbol, should it be escaped as I have it? I am not sure. Also, is the underscore needing escaping too? I cannot really test these as of now, since I thought I would need to make a new .htaccess file for the forum directory, so I went on to creating it. This one time i think i need to add
RewriteBase /forum/ So i think i need these changes for it to work? It will take some more time for godaddy to enable the new htaccess file on that dir... until then, i have all the time in the world for a cup o coffee hehehe thanks guys for all your help! (Especially Jim, who has mostly helped me singlehandedly) :D
You just need to make sure that the RewriteRule pattern matches the URL-path you want to rewrite from the point of view of this .htaccess file's location and that the substitution URL is the correct URL-path starting at the root of the site (start it at "/"). So, your pattern appears to be correct, but your substitution address is missing the "/forums/" path component.
The "#" and "_" characters do not need to be escaped in your patterns. However, you cannot use a "#" symbol in the URL itself. "#" symbols can be used in query strings attached to URLs, but cannot appear in the URLs themselves; They are reserved characters (See RFC 2396).
As such, if you use a "#" symbol in your URL, it will be hex-encoded by the client (browser or robot), and your new 'friendly' forum URLs will appear as example.com/forums/viewthread/123-45-67%23_89. So, I suggest that you leave the pound sign out of the 'friendly' URL, and just use your new rule to add it back into the query string during the rewrite.
Using this information, please modify the substitution address in your rule and test it. We can help if you have problems. We're happy to help you learn the concepts, but most contributors here really don't have enough free time to provide on-going 24/7 support.
Thanks,
Jim
The # part was something I wanted to add because i have the forums rigged with anchors on each post, so this will take the user to the exact post on the anchor... is there a way to make this happen with a rewrite rule? I know you mention RFC 2396, but in section 2.2 of RFC 2396 the pound symbol is not shown as a reserved character... maybe i am overlooking something!?
Leave the "#" out of the new friendly links appearing on your pages, using links such as <a href="example.com/forums/viewthread/123-45-67_89"> and add it back in using the RewriteRule, as I said above. To be blunt, I post in a very detailed style, and try to be very specific -- but without blathering on unnecessarily. I do this to save time, because keeping up with this forum takes a lot of time, and we are all volunteers here. Please read carefully.
Anyway, if you leave the "#" out of your friendly link, and leave it out of the RewriteRule pattern, but put it into the substitution as "#post_$4", it should work as you wish.
Jim