Forum Moderators: phranque

Message Too Old, No Replies

Godaddy URL rewriting

         

morales2k

6:04 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Hello gurus of the web!

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!

jdMorgan

6:15 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...all the details I can...

We need a few more: :)

  • Please give some examples of the URLs you want to rewrite to your script, using "example.com" as your domain.
  • Please specify the exact server filepath to your script.
  • How did you test your code? (Usually, "I typed xyz into my browser, and...")
  • What were the results? -- What did you see in your browser, and what did you see in your server access and server error logs?
  • How did your results differ from your expectations?

    There are a few more questions, but they concern tidying-up your code, and are not critical right now.

    Jim

  • morales2k

    6:36 pm on Aug 14, 2008 (gmt 0)

    10+ Year Member



    Ok! :)

    -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. :)

    jdMorgan

    7:04 pm on Aug 14, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Ah, you're understanding the whole thing backwards, then -- probably because of confusion between the difference between a URL on the Web and a filepath inside a server, and/or how and when RewriteRules are applied.

    If it is your intent to use "search-engine-friendly URLs," then this is how it works:

  • Change all links on all your pages to the new friendly URLs -- The links on your Web pages *define* the URLs.

  • Internally rewrite the new "friendly" URLs, when requested from your server, to the filepath of your script.

  • (Optional) Externally redirect any client (browser or robot) requests for the old "unfriendly" URL to the new friendly URL.

    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

  • morales2k

    7:13 pm on Aug 14, 2008 (gmt 0)

    10+ Year Member



    It is so good to see this blinding light illuminating my path! Thanks for the insight!

    I'll get back to this thread if I make any progress.

    morales2k

    7:27 pm on Aug 14, 2008 (gmt 0)

    10+ Year Member



    Ok, let me see if I got this right... I have the RewriteRules set alright, so all I do to test (the right way) is that I go to my site, and I type in example.com/index/readmore/193/ and it should use the rewrite rule to look up index.php?readmore=193... if this is the gist of it... it aint working right :( I get a msg saying: No input file specified.

    (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.

    jdMorgan

    9:17 pm on Aug 14, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I don't know how your image rule worked, since as you posted it above, it requires a trailing slash.

    You could correct that and shorten it up a bit:


    RewriteRule ^image/[a-zA-Z]+\.(gif¦jpg¦png)$ /dead-sig.gif [L]

    Note: Replace the broken pipe "¦" characters with solid pipe characters before trying to use code posted here; Posting on this forum modifies the pipe characters.

    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

    morales2k

    9:36 pm on Aug 14, 2008 (gmt 0)

    10+ Year Member



    I just read from another forum that this message comes from having PHP setup as CGI/FastCGI on the server.

    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. (?)

    g1smd

    1:06 am on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Your line that ends in [R] is a 302 redirect.

    I would change that bit to [R=301,L] personally, and fix up the domain as well at the same time in that one rule.

    jdMorgan

    1:15 am on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > Nothing showed on my access log.

    Be sure to completely flush your browser cache (Temporary Internet files if using Internet Explorer) before testing any new code.

    Jim

    morales2k

    3:07 pm on Aug 15, 2008 (gmt 0)

    10+ Year Member



    @g1smd:
    Hi thanks for your advice. The main idea on my first line is to add the last slash (/) to the url if a user enters the url manually without the last slash. So it redirects the url to the next rewrite rule... that has the last slash, and then it should rewrite to the script file on the server, which is not happening and I am getting the "No input file specified" message over a blank screen. In any case, you made me think about my approach, and I think it would be better if I follow this rewrite scheme instead:
    RewriteRule ^index/readmore/([0-9]+)\.htm$ index.php?readmore=$1

    I'd say that it is pretty much similar to what is used in here.

    @Jim:
    I am using FF2 (its clear private data for FF2), and even after doing what you adviced on IE7, still the same results.

    morales2k

    4:39 pm on Aug 15, 2008 (gmt 0)

    10+ Year Member



    UPDATE:

    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]

    jdMorgan

    5:00 pm on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You can probably delete the RewriteBase line, since "/" is the default.

    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

    jdMorgan

    5:16 pm on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    One more thing... If you have any problems using other modules and directives in .htaccess, you might want to change that Options directive to

    Options +FollowSymLinks -MultiViews -Indexes

    since it was probably the "MultiViews" that was causing your problem. The line above leaves all other Options enabled while enabling FollowSymLinks and disabling directory listings.

    Jim

    morales2k

    7:29 pm on Aug 15, 2008 (gmt 0)

    10+ Year Member



    Ok! This was successful. I managed to change most links here and there... everything is hardcoded now, as oposed to what I had before... since its a CMS it had a lot of variables, defining URLs, and Paths... so I changed whatever had to deal with images and links, and hardcoded them, since I am not changing my theme or whatever, the changes should be ok for a long time...

    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/

    Maybe that last slash is not needed?

    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

    jdMorgan

    7:53 pm on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You do not need to use RewriteBase. When needed, RewriteBase is used as a server-wide 'correction' to compensate for odd user-account-to-filespace mappings.

    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

    morales2k

    9:36 pm on Aug 15, 2008 (gmt 0)

    10+ Year Member



    Awesome! It works! I am totally getting the hang of this! Oh, this is so useful! I don't know how I managed to code without this thing! Maybe now I can improve my pagerank a notch or two... oh well... to the point.

    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!?

    jdMorgan

    9:54 pm on Aug 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Try it: Put a link on your page with a "#" in it, and then use the Live HTTP Headers add-on to see what is sent by the browser to the server. It will be "%23" and not "#", and that is what will show in your search engine listings as well -- It is ugly and "hard to read and type."

    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

    morales2k

    3:46 pm on Aug 20, 2008 (gmt 0)

    10+ Year Member



    Sorry for that Jim... I did not understand it the first time you explained it... Thanks a lot for your help. It works as expected now. Well, another novice has been serviced! Thanks a lot! For volunteer work, the quality of the support here is amazing. I'd be proud if I were you. :) You do an awesome job. It is a great service! On the name of those who ask and go away without thanking you... Thanks you, thank you, thank you for doing what you guys do on these boards! This goes to you (Jim), and all other contributors forum-wide as well! Keep up the great stuff!