1

I'm trying to hide the .php extension from my files as well as force a trailing slash on the resulting URLs.

Example: A request to /about.php would become /about/ and requests to /about would go to /about/.

The following rewrite code worked perfectly when I was in the root of my hostdomain:

RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://edit.mydomain.org/$1/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://edit.mydomain.org/%1  [R=301,QSA]

However, I need to move my files into a directory of this host name. Adding a directory name to the rules and having the .htaccess in the directory itself didn't work at all and seems to cause a endless redirect.

I looked around StackOverflow and other websites and tried numerous examples and ended up with many different errors with the most common being:

  • Everything is an endless redirect.
  • Everything except the directory home page is a 500 Error.
  • about.php redirects to /about but there's no redirect to /about/ and /about/ displays a 500 Error.
  • Everything working, but the home page (of the directory) index.php when accessed without a filename goes into an endless redirect.
  • Things redirect to edit.mydomain.org/home/username/public_html/mydomain.org/edit/pagename.php which obviously doesn't exist.

Thanks for any help! I really need to keep these files in a directory although the .htaccess could go into the host name root if its needed.

The directory for this would be edit.mydomain.org/dave/

2 Answers 2

3

Save this as a .htaccess and put it in the 'dave' directory

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]

RewriteRule ^(.*)/$ $1.php [L]
5
  • Thanks so much! That worked perfectly. I was also wondering how I could hide index.php when someone accesses the index file directly? Right now, it just redirects to /index/ and displays the home page. I added a redirect line I found for index.php but now its giving me an Apache "This Document has Moved" page although the link still goes to index.php.
    – Brian
    Apr 3, 2011 at 6:11
  • 1
    @Brian - If you don't want people accessing index.php, then change the directory index by adding this to the .htaccess after the RewriteEnginx On line: DirectoryIndex whatever.php index.php. Then when you point your browser to a folder Apache will first look for the whatever.php file and if it exists it will use that, if it doesn't exist it will then look for index.php. So you can change your index files to whatever.php anybody going to index.php will just get a 404 file not found error.
    – Emmanuel
    Apr 3, 2011 at 7:33
  • Oh, by the way, if you really don't want people to know you're using php make sure the expose_php setting is set to Off in your php.ini. See this link: goo.gl/JRIYp
    – Emmanuel
    Apr 3, 2011 at 7:37
  • Thanks again! Sorry for asking so many questions, but when people access edit.mydomain.org/dave directory without the trailing slash, it gets redirected to edit.mydomain.org/dave//home/username/public_html/mydomain.org/edit/hcj/ and since this is a server path, its a 404.
    – Brian
    Apr 3, 2011 at 16:01
  • @Brian - try adding this before your RewriteConditions in the .htaccess of your root directory RewriteRule ^dave$ http://edit.mydomain.org/dave/ [R=301,L]
    – Emmanuel
    Apr 3, 2011 at 20:10
1

This works for me

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
1
  • Awesome, thought I'd never find it. :) Apr 24, 2012 at 21:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.