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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This one has the added benefit of forcing all links to include the trailing slash.
but I don't have /just/ jobs and contact. how do I make it so that it accepts any page? Just replace it with *?
@DumbAsker Try replacing (jobs|contact) with ([^/]+)
Looks great, I'll try this out as soon as I can. My laptop bugged out and I'm still migrating stuff over to a new unit.
Unfortunately this doesn't work and actually gives me weird results. I would like to rephrase my question. Please see my edit.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.