Solved

removing page extension using mod_rewrite

Posted on 2009-07-08
9
597 Views
Last Modified: 2012-05-07
ok i have successfully removed my pages' extensions using mod_rewrite, but now I'd like to take it a step further. Right now, a page named "domain.com/example.php" is linked from my other pages as "domain.com/extension" and it works. If someone mistakenly links to the page with the trailing slash (ex: domain.com/example/) then my htaccess file removes the trailing slash from the url (redirecting to the no trailing slash version). The problem I have now is if the page is accessed with the extension (domain.com/example.php) it does not redirect to the correct format (no extension & no slash). Basically creating duplicate content in the eyes of Google. I know this isn't a big deal since no one will know the extension of the page, but better safe than sorry. Is there another htaccess snippet i can use to force the extension removal of every page (without doing this for each individual page) or a line of php i can insert into the header of the pages that redirects it to the correct format? thanks in advance. my current htaccess code is below...
Options +FollowSymlinks
RewriteEngine on
 
# canonical domain rewrites
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
 
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Open in new window

0
Comment
Question by:xill2678
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24812678
RewriteCond %{REQUEST_URI} !\.php$
0
 
LVL 1

Author Comment

by:xill2678
ID: 24817077
this snippet doesnt seem to be doing anything for me. is it suppose to replace something?
0
 
LVL 42

Expert Comment

by:David S.
ID: 24817352
It won't do anything by itself, since it's just a condition.

Try this:
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:xill2678
ID: 24817676
i tried adding this to what i have, and i tried replacing what i have with this snippet. one way removed the extension but caused a 404 error. the other way resulted in a redirect loop :(

i'm new to rewrites, so i'll post what i have so far. i'm sure it's something small that i'm overlooking or doing wrong. the snippet you gave is the bottom two lines
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
 
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]

Open in new window

0
 
LVL 42

Expert Comment

by:David S.
ID: 24818066
Does putting it first help?
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]
 
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Open in new window

0
 
LVL 1

Author Comment

by:xill2678
ID: 24818188
no, unfortunately that throws it into a redirect loop again
0
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 24818315
Try this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
 
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
 
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]

Open in new window

0
 
LVL 1

Author Comment

by:xill2678
ID: 24818819
that works perfectly!! THANKS :)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 24821309
> RewriteCond %{REQUEST_URI} \.php$
this may cause a rewrite loop, that's why I used ! for negation:)
I'm wondering that the accepted suggestion works.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question