0

I have files in my root folder. jobs.php, contact.php.

I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.

Also, I want jobs.domain.com to point to www.domain.com/jobs/

<IfModule mod_rewrite.c>
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
</IfModule>

This is what I've got so far, and as much as I try. I can't seem to get it to work.

Any help would be awesome.

EDIT:

I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.

clueless
  • 839
  • 1
  • 10
  • 24
  • `RewriteRule ^contact/$ contact.php` for job `RewriteRule ^jobs/$ jobs.php` – Dipesh Parmar Sep 11 '13 at 03:21
  • possible duplicate of [Removing the .php extension with mod\_rewrite](http://stackoverflow.com/questions/4908122/removing-the-php-extension-with-mod-rewrite) – xdazz Sep 11 '13 at 03:24

1 Answers1

2

Your condition:

RewriteCond %{REQUEST_FILENAME}\.php -f

will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:

RewriteEngine On

# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]

RewriteRule ^([^/]+)/$ /$1.php [L]

# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]

Oh and I forgot:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]

Assuming that *.domain.com has the same document root as www.domain.com.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220