www.webdeveloper.com
Results 1 to 9 of 9

Thread: Redirect index.html and remove file extensions not working

  1. #1
    Join Date
    Mar 2014
    Posts
    5

    Redirect index.html and remove file extensions not working

    hi,

    I've been banging my head against the wall trying to figure this one out. I would like to do the following:

    1. Direct www.homepage.com/index.php to redirect to www.homepage.com ONLY

    2. Remove any .php or any type of extensions from the URLS.

    Can someone help me out here?

    This is what I'm currently using:

    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.a1aatlantic.com/$1 [R=301,L]


    This correctly re-directs the www.homepage.com/index.php to www.homepage.com. But what it also does is redirect any other page without the .php extension to the homepage also. All other pages without the .php extension should redirect back to the original page and not the homepage.

    Any suggestions?

  2. #2
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101
    That's because the main root is the "index.php" page file.
    What happens if you place an index.asp which redirects to a secondary php page?
    Last edited by MrSnowDrop; 03-11-2014 at 09:57 AM.

  3. #3
    Join Date
    Mar 2014
    Posts
    5
    Quote Originally Posted by MrSnowDrop View Post
    That's because the main root is the "index.php" page file.
    What happens if you place a "blank" index.html (or index.asp) page?
    If you need to maintain the php page, you could redirect from the index.asp (possibly renaming the php)...
    Thanks for the response. Unfortunayely I'm not very good at this, so excuse the noobness. It currently works fine for the homepage, but every other page without the .php gets re-directed to the homepage. Is there no way to keep everything redirecting to the same page?

  4. #4
    Join Date
    Mar 2011
    Location
    Rome, Italy
    Posts
    101
    Try to rename index.php to "home.php";
    then add an "index.asp" page along with a vbscript redirect code to the home.php (see w3schools for the response.redirect vbscript example code).

  5. #5
    Join Date
    Mar 2014
    Posts
    5
    wow, I think that's beyond my capabilities ((

    Cant I do a simple 301 re-direct of the /index.php page to the homepage?

    And then implement another re-write to remove all the .php extensions from the pages?

  6. #6
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    946
    I can't really test the entire .htaccess file right now, but you're looking to add something along the lines of:
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+)$ $1.php [L,QSA]
    This basically will take a url such as http://www.randomsite.com/about-us and if 'about-us' is not a folder, it will append .php and load the page, but leaving the URL in the browser the same.

    More than likely you will want this above your current rewrite statements (I have no idea how it should be positioned in relation to the +FollowSymLinks), as this will not load anything if the page doesn't exist at all (eg. if there is no 'about-us.php' on the server, the rewrite rule does nothing and thus would continue on to your rule, bringing the user to the homepage as a sort of fallback for bad links).
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  7. #7
    Join Date
    Mar 2014
    Posts
    5
    Quote Originally Posted by Sup3rkirby View Post
    I can't really test the entire .htaccess file right now, but you're looking to add something along the lines of:
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+)$ $1.php [L,QSA]
    This basically will take a url such as http://www.randomsite.com/about-us and if 'about-us' is not a folder, it will append .php and load the page, but leaving the URL in the browser the same.

    More than likely you will want this above your current rewrite statements (I have no idea how it should be positioned in relation to the +FollowSymLinks), as this will not load anything if the page doesn't exist at all (eg. if there is no 'about-us.php' on the server, the rewrite rule does nothing and thus would continue on to your rule, bringing the user to the homepage as a sort of fallback for bad links).
    Thanks for that. The homepage works perfectly, but now the other .php pages do not redirect to without the .php URLs. So now all the other pages have both, .php and without .php URLs. Here's what I have:

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

    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]

  8. #8
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    946
    It looks like you just need to adjust the order of some things. I don't know if I can tell you exactly why it isn't working for you, but I did just test the snippet I gave you and it is working just fine on my site.

    As far as I can tell right now, it just seems that another part of your .htaccess file is interfering with the snippet I gave you. I can't really test your specific case, all I can do is take some guesses and let you try them out. This next snippet of code should do 3 things: 1) accept urls missing '.php' as the extension (if the file exists), 2) redirect 'example.com' to 'www.example.com' (matching http and https), 3) remove 'index.php' from any url (as it simply indicates the base/index file for a given directory).
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)$ $1.php [L,QSA]
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteRule ^index.php$ / [L]
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  9. #9
    Join Date
    Mar 2014
    Posts
    5
    Thanks a ton for your help. I tried what you gave me and it did not work. I'm not that well versed with this, so I've posted everything from my HTACCESS below. Basically, my goal is to (a) re-direct www.example.com/index.php to www.example.com (b) re-direct all other pages that end with .php to their respective pages such as www.example.com/about.php to www.example.com/about and (d) set the preferred URL canonical to www.

    # -FrontPage-

    IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

    <Limit GET POST>
    order deny,allow
    deny from all
    allow from all
    </Limit>
    <Limit POST PUT DELETE>
    order deny,allow
    deny from all
    </Limit>
    AuthName www.example.com

    ErrorDocument 500 http://www.example.com

    ErrorDocument 404 http://www.example.com

    ErrorDocument 403 http://www.example.com

    ErrorDocument 401 http://www.example.com

    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles