Help with Removing File Extentions

New to mod_rewrite? This is a good place to start.

Help with Removing File Extentions

Postby Gus » Sun Sep 27, 2009 4:32 pm

Hi,

Over the last week I have casually been browsing around the forum trying to understand exactly what I need to do to get my urls as I want them to be. I'm new to this as well, so please be patient with me! :)

I have a number of pages such as these,



Its a new site so there is no risk of breaking links etc.

index.php -> domain.net/v5/

work.php -> domain.net/v5/work/one/

about.php -> domain.net/v5/about/

blog.php -> domain.net/v5/blog/

contact.php -> domain.net/v5/contact/


So if a user enters domain.net/v5/about.php I would like to force the url to change to domain.net/v5/about/ and of course for /about/ to work as if it was a dir with a page. I would also like to keep the / at the end if possible.

(I'm currently working with the page in the v5 dir as it is not ready and will eventually be removed)

Any help in regard to what I want to achieve would be greatly appreciated! If I have left any essential information out please just ask.

Regards
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Postby richardk » Mon Sep 28, 2009 2:18 pm

If you want to do it for all PHP files there's FAQ: Removing (.php) file extensions.

You will need a specific rule for
work.php -> domain.net/v5/work/one/

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^work\.php$ /v5/work/one/ [R=301,L]

RewriteRule ^work/one/?$ /v5/work.php [QSA,L]

It will need to go before the FAQ rules/conditions.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gus » Mon Sep 28, 2009 2:41 pm

Ah that is great! Thank you.

How would I go about setting these other ones up manually? As it I will need to have access to some php files myself. Removing extensions and adding the trailing slash..

Thanks!
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Postby richardk » Mon Sep 28, 2009 3:43 pm

(Nearly) the same as the specific example
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(about|blog|contact|index)\.php$ /$1/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^work\.php$ /v5/work/one/ [R=301,L]

RewriteRule ^(about|blog|contact|index)/?$ /$1.php [QSA,L]

RewriteRule ^work/one/?$ /v5/work.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gus » Mon Sep 28, 2009 5:06 pm

I uppdated with your suggestion, the work.php file changes perfectly to /work/one/ but the others do not.

If I go to the contact.php file it just displays the content, but /contact/ works too. Same with index.php which stays..

Thanks!
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Postby Gus » Tue Sep 29, 2009 5:06 am

Would something like this be fine?

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ /v5/$1 [R=301,L]
RewriteRule ^/?$ /v5/index.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about\.php$ /v5/about/$1 [R=301,L]
RewriteRule ^about/?$ /v5/about/$1.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^contact\.php$ /v5/contact/$1 [R=301,L]
RewriteRule ^contact/?$ /v5/contact/$1.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^contact\.php$ /v5/contact/$1 [R=301,L]
RewriteRule ^contact/?$ /v5/contact/$1.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^work\.php$ /v5/work/print/ [R=301,L]
RewriteRule ^work/print/?$ /v5/work.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^identity\.php$ /v5/work/identity/ [R=301,L]
RewriteRule ^work/identity/?$ /v5/identity.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^web\.php$ /v5/work/web/ [R=301,L]
RewriteRule ^work/web/?$ /v5/web.php [QSA,L]


What if i want to force /v5/work/ to go to /v5/work/print/ ?

Thanks :)
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Postby richardk » Tue Sep 29, 2009 2:56 pm

You shouldn't need to change (about|blog|contact|index). That means it will work for all of the listed files. If you have to expand it, you would do it exactly like the /work/one part. Try this, but i don't think the (|||) was the problem.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about\.php$ /about/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^blog\.php$ /blog/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^contact\.php$ /contact/ [R=301,L]

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^work\.php$ /v5/work/one/ [R=301,L]

RewriteRule ^work/?$ /v5/work/print/ [R=301,L]

RewriteRule ^about/?$ /about.php [QSA,L]
RewriteRule ^blog/?$ /blog.php [QSA,L]
RewriteRule ^contact/?$ /contact.php [QSA,L]
RewriteRule ^work/one/?$ /v5/work.php [QSA,L]

index.php is a special case.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gus » Wed Sep 30, 2009 10:47 am

Thanks so much richardk, it has been to much help! One last question,

It is in regard to /work/ to force /work/print/ which does not seem to work, I have highlighted what relates to it in bold.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about\.php$ /about/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^blog\.php$ /blog/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^contact\.php$ /contact/ [R=301,L]

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^work\.php$ /v5/work/print/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^identity\.php$ /v5/work/identity/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^web\.php$ /v5/work/web/ [R=301,L]
[b]
RewriteRule ^work/?$ /v5/work/print/ [R=301,L][/b]

RewriteRule ^about/?$ /about.php [QSA,L]
RewriteRule ^blog/?$ /blog.php [QSA,L]
RewriteRule ^contact/?$ /contact.php [QSA,L]
RewriteRule ^work/print/?$ /v5/work.php [QSA,L]
RewriteRule ^work/identity/?$ /v5/identity.php [QSA,L]
RewriteRule ^work/web/?$ /v5/web.php [QSA,L]


For some reason I don't get that to work, but it is only when the trailing / is added that it does not force the /work/print/

These do work, and force /work/print/

Code: Select all
/work
/work.php



Thanks!
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Postby richardk » Wed Sep 30, 2009 1:13 pm

Try putting that rule first and replacing
Code: Select all
Options +FollowSymLinks

with
Code: Select all
Options +FollowSymLinks -MultiViews
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gus » Wed Sep 30, 2009 2:16 pm

Hi richard!

Thank you! Works great! Such a small thing.

I was reading this thread and the posts by mescon, viewtopic.php?t=42363

And I must agree to 100%. Please set up a little paypal donations section or amazon wishlist, So we all can show our appreciation of the work and effort you put in around here!

-Gustaf
Gus
 
Posts: 7
Joined: Sun Sep 27, 2009 4:11 pm
Location: London

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron