SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess SEO URLs

    Hi,

    I have this in my htaccess, now it this is for removing the .php extension. My subdomain is http://example.domain.com and each time htaccess tries to look for .php and shows up 404, I've trying to find out how to make this work as well add trailing slash http://www.domain.com/example/ when ever going to the subdir.

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]

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

    Your code is fine (except for the No Case flag) for adding a .php extension to an extensionless filename. Apache will add the / to give you domain names but you will also need to check for !-d for the PHP extensionless, too. If you "merely" want to add the trailing / for directories, try:

    Code:
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule !/$ %{REQUEST_URI}/ [R=301,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

  3. #3
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    ix,

    Your code is fine (except for the No Case flag) for adding a .php extension to an extensionless filename. Apache will add the / to give you domain names but you will also need to check for !-d for the PHP extensionless, too. If you "merely" want to add the trailing / for directories, try:

    Code:
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
    Regards,

    DK
    I see, the rules given seems to block me from my site. The above code works, but tries to add ".php" to a directory subdomain and it gives off a 404 error. As per the trailing / can it be added to files .php or the files must be created within a folder of the same name?

    Do you know where I could possibly get a list of commands? Thanks.

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

    The rule above was designed to add a trailing / to a directory URI.

    Why would anyone want to add a trailing / to a file name? This is both illogical and contradicts the functioning of the server. It IS possible, however, if you use MultiViews (which I loathe).

    As for a good tutorial, I've had acclaims from many SP members for the one linked to in my signature (and you know where to find me if you have questions about the content).

    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

  5. #5
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The rule given above blocks me from my site.

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

    Troubleshooting help requires the test URI as well as the code it's being run against.

    Code:
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
    ... simply says that, if the URI is a directory request and there is not a trailing /, add the trailing /. There is NOTHING there which would block anything.

    So, please post your full .htaccess and test URIs.

    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
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dklynn View Post
    ... simply says that, if the URI is a directory request and there is not a trailing /, add the trailing /. There is NOTHING there which would block anything.
    not quite, it checks if it is an EXISTING directory:

    '-d' (is directory)
    Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
    httpd.apache.org/docs/2.2/mod/mod_rewrite.html

    (bold is mine)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    I thought something could be added onto mine above to make it add that trailing /

    The other issue is I cannot access my subdomain because it would give a 404 error for trying to add .php

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

    Rémon is (pedantically) correct as you don't want to redirect to a directory which does not exist. The code functions in the manner he described.

    Your code says: If the {REQUEST_URI} is not for an existing file, add .php to the URI and serve that (REMOVE that No Case flag - URIs are case sensitive!!!). I do not know how you added a trailing / with this code.

    Because your two cases are different (one is add .php file extension - to a non-file, i.e., DIRECTORY, name!?! - and the other is add a trailing / to a directory). As I had stressed in another current thread, your first step should have been to define each task for mod_rewrite, convert each of those tasks into pseudocode (to see where conflicts might exist) then into mod_rewrite code. You have skipped the first step and your results show it. Relax, think about each task in detail THEN get to coding.

    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

  10. #10
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might you know where to find the rules given with a description, I can go off there and put stuff together.

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

    I use apache.org as my reference for all things mod_rewrite. They're generally readable but do get into some techno-babble but well worth wading through.

    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
  •