Forum Moderators: phranque

Message Too Old, No Replies

Trailing Slash (for the ump-teenth time)

Having trouble with HTACCESS and trailing slashes

         

MikesPad

4:03 pm on Jun 19, 2011 (gmt 0)

10+ Year Member



Hey guys,

- I have read lots of the other posts on trailing slashes, and removing file extensions, but can't seem to figure it out (sorry about that).

I am working on a site, and have a seemingly simple structure (no variables or ?id=xyz, etc).

My goal is to basically remove all .php endings from my files, and still have the site function like normal.

[dupreeandcompany.com...] works perfectly (but i dont want end user seeing the PHP portion)
[dupreeandcompany.com...] works perfectly
[dupreeandcompany.com...] currently directs to -> [dupreeandcompany.com...]

Here is the HTACCESS code i have in there now:
-------------------------------------
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

<Files 403.shtml>
order allow,deny
allow from all
</Files>
-------------------------------------

lucy24

4:26 pm on Jun 19, 2011 (gmt 0)

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



I think your htaccess is doing the opposite of what you want.

#1 A rewrite without accompanying redirect sends the user to a different physical location but doesn't affect what they see in the address bar.

#2 A redirect has to send the user to a page that actually exists, or else they will end up on your 404 page (which is really a special kind of rewrite).

#3 Internal links have to point to real locations within your site. Don't tell people they're going to one place and send them to another. Rewrites are for requests that you have no control over, like outdated bookmarks or external links.

#4 No matter what the user sees, the actual files will have extensions.

#5 If what you're trying to do is send people to a file that really has .php at the end, but make the address bar look as if there isn't one, you are asking for trouble. To put it mildly.

MikesPad

5:51 pm on Jun 19, 2011 (gmt 0)

10+ Year Member



Basically what i want is to simply remove file extensions. I know many people do this, as most sites you visit these days do not show them. Any help in executing this with the least amount of code possible would be greatly appreciated. Thanks.

g1smd

6:47 pm on Jun 19, 2011 (gmt 0)

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



Rather than type answers again, this from a few hours ago: [webmasterworld.com...]

You remove the .php by removing it from the links on your page. List the redirects before the rewrites. The rewrite accepts a URL request without .php and internally rewrites to file with .php.

You also add a redirect before the rewrite. The redirect accepts an external request with .php in it and redirects the user to a different URL - one without .php in it. The redirect needs to test THE_REQUEST in order to avoid an infinite loop.

Use example.com to stop forum auto-linking.

MikesPad

9:09 pm on Jun 19, 2011 (gmt 0)

10+ Year Member



Do you have the code i can paste into the HACCESS file? I went to that link and read your description (And understood about half of it). but didnt see anyone provide the code needed to "internally rewrite the file with .php". thanks if you have that code and can share it here.

g1smd

10:16 pm on Jun 19, 2011 (gmt 0)

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



Based on what you have read so far, let's see your best-effort code. There are close to two thousand threads in this forum with discussions on this and closely-related topics.

The forum exists to help you improve your coding skills, based on your research. It does not exist as a free code writing service, there just aren't enough volunteers with enough free time.