Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Question Changing site.com/test.php --> site.com/test

    So I'd like to change urls like www.site.com/test.php to be www.site.com/test/

    I'm sure this is .htaccess related, so could someone please explain? I tried googling, but I don't know what to really call this.

    Any help is appreciated.
    Last edited by Blatch; 03-29-2009 at 07:15 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    That's right, you can use htaccess to make it work: http://newmediarts.blogspot.com/2007...urls-with.html

  • Users who have thanked CaptainB for this post:

    Blatch (03-27-2009)

  • #3
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Works great! Thanks!

    But how would I go about having extended urls such as:

    http://www.site.com/test/page1

    ?

  • #4
    Regular Coder
    Join Date
    Mar 2009
    Location
    Troutdale, OR
    Posts
    147
    Thanks
    12
    Thanked 6 Times in 6 Posts
    I simply use folders to achieve this. I think its called search engine friendly links or removing file extension in url
    The thing about .htaccess, it deletes all your file extensions including images and other things and you don't want that. Search engines likes .jpg / .png /.gif and other helpful extensions as far as I know, and they don't like things like .html / .php / .aspx. So I simply use folders. Every page has its own folder. (good organization training btw )

    For example:
    I want to rename my /contact.php form to /contact
    What I need is /contact folder and than I put contact.php in it. There are one more thing to it, /contact.php has to be remained to /index.php, this way ftp will read it as main folder file.

  • #5
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    I'd rather not have to make multiple folders because I'm running a .tpl system.

    I'd like to be able to get a url like this:

    http://www.gamexe.net/3d/disturbed-logo-part-3/

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The suggested solution is a very poor way of accomplishing the functionality you'd like.

    You'll want to look into using regular expressions in your rewrite rules.

    Code:
    RewriteEngine On
    
    RewriteRule ^(\w+)/+page([0-9+])/*$ /inc/$1.php?page=$2 [L]
    RewriteRule ^(\w+)/*$ /inc/$1.php?page=1 [L]
    
    ErrorDocument 404 /inc/error.php
    
    # Examples:
    # http://www.site.com/whatever/page3 -> http://www.site.com/inc/whatever.php?page=3
    # http://www.site.com/whatever       -> http://www.site.com/inc/whatever.php?page=1
    Last edited by kbluhm; 03-28-2009 at 08:46 PM.

  • Users who have thanked kbluhm for this post:

    Blatch (03-29-2009)

  • #7
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    100% agreed with kbluhm, mod_rewrite is a very effective means of making clean URLs while not sacrificing functionality.

    Googled

    I custom built a CMS I use on the sites I design that is based around this concept. It allows me to run everything through index.php (much more efficient for things like global includes, session management, page management, etc.) and output it under seemingly different sections.

    For example in my case I've got:
    http://mydomain.com/page/about-us/27

    In the address bar, while mod_rewrite is actually turning that into

    http://mydomain.com/index.php?p=about-us&articleID=27

    Script handles it, user is happy. All good times
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    Blatch (03-29-2009)

  • #8
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Let Me Google That For You

    the methods above are the best and they are the correct ways to use mod_rewrite

  • #9
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Those look as if they turn

    http://www.mydomain.com/page/about-us/27

    into

    http://www.mydomain.com/index.php?p=...s&articleID=27

    How do I do it the other way around?

  • #10
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Quote Originally Posted by Blatch View Post
    Those look as if they turn

    http://www.mydomain.com/page/about-us/27

    into

    http://www.mydomain.com/index.php?p=...s&articleID=27

    How do I do it the other way around?
    Well it does both. See what happens is this:

    The Server (aka Apache) doesn't understand http://mydomain.com/page/about-us/27. To that it will understand it as folders and sub-directories.

    So, in order for Apache to understand it, the mod_rewrite transforms that into the long string listed above according to your re-write rules.

    So essentially what you're doing is all the links the user sees are the nice, clean ones. The only time the long index.php?p=about...etc. links appear are internally to apache - never seen by the user.

    To achieve this, you use mod_rewrite. For example lets say I've got this architecture:

    http://mydomain.com/index.php?p=my-p...ticleNumber=38

    I don't want the user to see that mess, so I use mod_rewrite.

    I want users to type it in this format: http://mydomain.com/page/<page-name>/<article-number>. So lets tell apache that's what we want:

    Code:
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^page/(.*)/(.*)$ index.php?p=$1&articleNumber=$2
    Definitions:
    (.*) - This is REGEX essentially meaning that it accepts any character into that spot within the link
    $ - In the inital statement this signals the end of the REGEX expression
    $1... $2... $n - This signals the regex expression you are relating to. So the first instance of (.*) is $1, the second instance is $2, and so on.

    That's a quick run-over of it for you. If you are interested in learning it, I highlighy recommend you check out this tutorial. I used that one to start teaching myself mod_rewrite and it is very helpful.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    Blatch (03-29-2009)

  • #11
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks so much for the help. :]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •