SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast catch's Avatar
    Join Date
    Sep 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess, remove .php and add / to end of url

    How do I get my urls to look like:

    www.example.com/test/

    instead of www.example.com/test.php

    and to make it work in sub directories too like:

    www.example.com/test2/file/

    instead of www.example.com/test2/file.php

  2. #2
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you rename test.php to just 'test' and then make an .htaccess file with the following:

    <Files test>
    ForceType application/x-httpd-php
    </Files>

    It will work, the only trouble is that for each additional file that works like this you need to duplicate that part of the .htaccess

    It depends how many files you want to handle like this really..

    For two files you can do this:

    <Files test>
    ForceType application/x-httpd-php
    </Files>
    <Files test1>
    ForceType application/x-httpd-php
    </Files>

    And for subdirectories you can just create a .htaccess in the subdirectory in the same way.

    Hope that helps!

  3. #3
    SitePoint Enthusiast catch's Avatar
    Join Date
    Sep 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it possible to remove the .php through the .htaccess instead of renaming each file?

  4. #4
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A quick search of google for 'clean url' found a few things that might be useful:

    http://wettone.com/code/clean-urls

    http://www.tutorio.com/tutorial/php-...-friendly-urls

    http://www.slipszenko.net/tutorial:Making+Clean+URLs

    Hope that's helpful

  5. #5
    SitePoint Enthusiast catch's Avatar
    Join Date
    Sep 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried google but I can't seem to find what I mentioned in my 1st post, the links you give do work but don't add the / at the end of the url.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    catch,

    Best to have a read of the Article in my signature as it answers your questions.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Enthusiast catch's Avatar
    Join Date
    Sep 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn,

    I checked out your site so far I got in my .htaccess

    RewriteEngine on
    RewriteRule ^/?([a-z]+)$ $1.php [L]

    now that works when I type

    www.example.com/test

    but when I type:

    www.example.com/test/

    it doesn't, and I can't find out how to add the / on the end of the URL... on your website.

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    catch,

    If you use the trailing /, it can be confused as a directory request. However, mod_rewrite IS up to the task of handling either situation by adding an optional slash before the end anchor. Just be aware that EVERY subdirectory request without a file in the path will be treated this way, too! In other words, that's NOT recommended.
    Code:
    RewriteEngine on
    RewriteRule ^/?([a-z]+)/?$ $1.php [L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Enthusiast catch's Avatar
    Join Date
    Sep 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alright, thanks

  10. #10
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    how i can rewrite my urls to static ones.
    Basically i don't know how to write regular expressions thats why i am writing down the url structure and the result i am looking for.

    http://www.myDomain.com/se/search_re...?search=iphone

    I want to make it:
    http://www.myDomain.com/se/search/iphone

    OR

    More better if some one can give me a .htaccess file that will rewrite url to
    http://www.myDomain.com/se/iphone

    Thanks in advance.

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    fb,

    That's ALL in the tutorial Article linked in my signature.

    Please read threads (in this thread, it's post #6) and you'll get that same advice.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  12. #12
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    I read your tutorial and used the following code in my .htaccess

    RewriteEngine on
    RewriteBase /se

    RewriteRule ^search/(.*)/?$ search_results.php?search=$1 [NC,L,QSA]

    But still the url is not re-written.
    Can you please help.

    Thank

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    fb,

    If you'd read it, you would know to NEVER use (.*) - it's just lazy code which will backfire on you 99 times out of 100. Also, I hope that I'd stated in there that RewriteBase is ONLY to correct a Redirect problem. Saying that,
    Code:
    RewriteEngine on
    RewriteRule ^/?se/search/([a-z]+)/?$ se/search_results.php?search=$1 [L]
    Your optional trailing slash will NEVER be matched (because of the (.*)) and No Case and Query String Append flags are NOT appropriate here.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  14. #14
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    I am a beginner and i am not sure where i am making mistake.

    If you mean that i put the above code:
    RewriteEngine on
    RewriteRule ^/?se/search/([a-z]+)/?$ se/search_results.php?search=$1 [L]

    in my .htaccess then i just did it but again it didn't worked.

    You can see the demo of my script at:
    http://www.projectsupdates.com/shopping_api/

    I will be thankful for your help in solving my problem.

    Thanks

  15. #15
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, my .htaccess file contains:

    RewriteEngine on

    RewriteRule ^/?shopping_api/search/([a-z]+)/?$ shopping_api/search_results.php?search=$1 [L]

  16. #16
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    fb,

    I believe that your problem stems from not having an action for your form! As a POST method, the value of the search item will be hidden from Apache (merely passed through with the POST array and not subject to review by mod_rewrite.

    IF you wanted to use the search term, you would have to use the GET method OR process the action in a JavaScript to create the desired URI format before SUBMIT.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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