Forum Moderators: phranque

Message Too Old, No Replies

.htaccess url rewrite

         

akjackson1

5:40 am on Nov 12, 2008 (gmt 0)

10+ Year Member



I've got a website that has the pages like domainname.com/index.php?home and index.php?about etc.
how would i go about rewriting it to:
domain.com/home
and
domain.com/about etc. etc.

I really haven't done anything with mod rewrite at all. so please make it simple!

g1smd

12:13 pm on Nov 12, 2008 (gmt 0)

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



This question (or something very close) has been asked at least 4 times in the last 48 hours.

Please check all the threads posted in the last two days for examples already discussed, and then post your best example code efforts here for discussion.

jdMorgan

3:10 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is, it's not simple... :(

See this thread [webmasterworld.com] for all the details.

Jim

akjackson1

3:39 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



oh, sorry about that. thanks for the link :)

could this possibly work? i relize i have to change my links, but just wondering if it would work, i have no idea how the syntax is suppose to go lol

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/?$ /index.php?$1 [L]

thanks

jdMorgan

5:11 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whether it might work depends on many details, but see the warning in this current thread [webmasterworld.com].

Jim

akjackson1

5:47 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



yeah it doesn't work :( i get an internal server error...

jdMorgan

5:52 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... so what does your server error log say?

Jim

akjackson1

5:58 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



have no idea what this means!
[11-Nov-2008 11:57:16] PHP Warning: Cannot load module 'pdo_mysql' because required module 'pdo' is not loaded in Unknown on line 0
that's a php warning though, so i don't know why i would get a 500 when i do stuff in htaccess...?

jdMorgan

6:17 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, not very useful. I don't know why you're not seeing an error in your server log, since the line you posted is a Warning, not an Error.

The first problem with your rule is that it contains nothing to prevent a request for index.php from being rewritten to itself. This sets up an 'infinite loop' -- one sure cause of a 500 error.

One of several possible cures --and the most efficient-- is:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond $1 !^index\.php$
RewriteRule ^([^/]+)/?$ /index.php?$1 [L]

That is, rewrite the URL-path "xyz" to "/index.php?xyz" unless the URL-path is already "/index.php".

In case it's not clear, mod_rewrite in .htaccess is recursive -- as it must be to enforce access restrictions and to detect further rewrites on previously-rewritten URLs. Therefore, recursion must be explicitly prevented.
Jim

akjackson1

6:37 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



:( i don't understand, maybe i shouldn't be url rewriting... i put that code in, but whatever i type in: /index.php/home, index.php?home, /home, /?home i just get my site with no styling...

akjackson1

6:38 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



see this here is what i want:
http://example.com/home
it is recognizing that the var $home is set, but i don't get any styling...

[edited by: jdMorgan at 7:05 pm (utc) on Nov. 12, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]

g1smd

6:52 pm on Nov 12, 2008 (gmt 0)

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



If you see the words, but not the styling, then you need to alter your links that point to your CSS files to now start with a / and include the full path, because it is the browser that resolves the URLs of the stylesheets based on the URL shown in the browser URL bar - which has now changed.

jdMorgan

7:04 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be typing in "http://www.example.com/home"

This is the link that you should put on your pages, and this link will be displayed in search engine results.

If that's not the case, then perhaps you're going about this (or possibly thinking about this) backwards.

Facts:

  • The URL you use on your pages defines the URL.
  • mod_rewrite cannot change the URL that appears on your pages; It can redirect requests for the linked URL, or it can internally rewrite requests for that URL to a non-default filepath, but it can't affect your actual on-page code.
  • The URL-path on the left side of the RewriteRule will be rewritten to the filepath on the right. And note the specific "URL-path" and "filepath" terms, as URLs and files are two very different things.

    So maybe it's time to back up a step and tell us what you want to do:

    What URL do you want to link to on your page? (this URL will appear in search results)
    Please confirm that the file that requests for this URL are to be rewritten to is /index.php in the root directory of your domain.

    Be aware that by introducing a virtual directory into the URL (which is what you are doing if you append a slash to that friendly URL and type it in as "example.com/home/"), you will be changing the base directory that the browser will use to resolve page-relative links on your pages. If you choose to use the trailing slash in your links (which I strongly recommend against in this case), you will need to change those page-relative links on your pages to server-relative links or to canonical links. That is, you'll have to
    use <img src="/images/logo.gif"> or <img src="http://www.example.com/images/logo.gif">
    instead of <img src="images/logo.gif">

    BTW, the thread I linked to above ("for all the details") describes all of this, and I hope you have read it... There are no "safe" short-cuts with this stuff, and if you make one little typo, you place your site's search ranking at risk. Please research before coding...

    Jim

  • akjackson1

    7:05 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    that makes 100% sense, the weird thing is, the php config, and style.css are in the same directory :S

    g1smd

    7:11 pm on Nov 12, 2008 (gmt 0)

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



    Where they are on the server has no bearing on this, it is what they are accessible by as URLs that over-rides all.

    akjackson1

    7:14 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    here's the code:
    <link href="style.css" rel="stylesheet" type="text/css" />

    akjackson1

    7:15 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    is href="/style.css" should work? i tried it and it doesn't...

    jdMorgan

    7:27 pm on Nov 12, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Well, be aware that like all other files in your root except for index.php, style.css will be rewritten to index.php?style.css

    You didn't say you didn't want that, and the rule you posted (with the correction I provided) will do that.

    18 posts on, and no progress... As I said, you need to stop coding, and define what you want to do precisely before proceeding to code.

    Jim

    akjackson1

    7:31 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    oh yeah, i see... how do i change it to rewrite ? to / but not / to ?... i think i'm getting myself all confused :O

    akjackson1

    7:39 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    putting the css directly inside the file fixed the problem, works perfectly now! thanks a lot... although it's kinda nice to have the css in an external file...

    jdMorgan

    7:41 pm on Nov 12, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You need to "make a map" of all of the URLs in your top-level directory, including these new "virtual" URLs like "/home" and all images, stylesheets, external JavaScripts, robots.txt and sitemap.xml files -- everything, and then decide which are to be rewritten to the script, and which are not.

    Again, note that I said URLs, not filenames, and the place to look for info to build this "map" is in the source code of your HTML pages.

    I suspect that you'll be able to "boil down" that map to a simpler form; For example, it may be that you only want to rewrite URLs to your script if they do not end with a filetype. But we cannot tell you that, you have to tell us, since only you are familiar with your sites' URL-space, with how it works today, and with how you want it to work...

    The reason for my first reply in this thread should be fairly obvious by now, but the fact it, you're going to be getting a lot more familiar with how servers work, and this will make things far easier in the future.

    Jim

    [edit] Fickst mi speling errurs [/edit]

    [edited by: jdMorgan at 7:47 pm (utc) on Nov. 12, 2008]

    akjackson1

    7:48 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    thanks so much for taking your time to help a poor 14yr old kid like me =D

    the only files i don't want to be rewritten are ones that end in .png, .jpg, .gif, and .css...

    jdMorgan

    7:56 pm on Nov 12, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    So you do want to rewrite robots.txt and sitemap.xml? Be very, very careful, here...

    Jim

    akjackson1

    8:02 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    i don't have robots.txt and sitemap.xml in my directory

    jdMorgan

    8:06 pm on Nov 12, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Weel, you certainly should, and coding the rule as you requested would mean you would never be able to add those files -- files which all properly-configured modern Web sites should have, BTW...

    Here, I'm going to suggest this because I'm off to take care of business for awhile:


    Options +FollowSymLinks
    RewriteEngine on
    #
    # Internally rewrite all top-level requested URLs which
    # do not end with a filetype to the /index.php script
    RewriteCond $1 !\.[a-z0-9]+$ [NC]
    RewriteRule ^([^/]+)$ /index.php?$1 [L]

    Note that this rule does not support ending your virtual URLs with slashes, and I strongly recommend that you leave it that way for the reasons noted above.

    Jim

    akjackson1

    8:14 pm on Nov 12, 2008 (gmt 0)

    10+ Year Member



    it works! thanks soo much, i really appreciate it :)