homepage Welcome to WebmasterWorld Guest from 54.161.192.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 57 message thread spans 2 pages: 57 ( [1] 2 > >     
Help With Redirects In .htaccess files
bluewaves



 
Msg#: 4547995 posted 8:04 pm on Feb 22, 2013 (gmt 0)

RewriteCond %{HTTP_HOST} ^sampledomain\.com
RewriteRule ^(.*)$ [sampledomain.com...] /$1 [R=permanent,L]

What does the above code actually do?

What I am looking for is a way to make all these
www.sampledomain.com/index.php
www.sampledomain.com/

go to
[sampledomain.com...]

I am not sure if the trailing slash should be the main target or the .com should be the main target.

Can anyone explain this to me?

Thanks in advance.

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 8:27 pm on Feb 22, 2013 (gmt 0)

The code you supplied redirects non-www to www but fails to redirect many of the other non-canonical versions.

Say in plain English what you want to happen.

I assume you want URL requests for example.com/index.php on any hostname to be redirected to the URL www.example.com/

I assume you want all internal pages requested on anything other than www.example.com to be redirected to the same page on www.example.com

I assume you want requests for the URL www.example.com/ to be fulfilled by the /index.php file, without revealing the name of that internal file.

Please confirm the above and fill in the rest of the gaps.

The rule you supplied will need to be improved and one or more other rules will need to be added.

bluewaves



 
Msg#: 4547995 posted 8:51 pm on Feb 22, 2013 (gmt 0)

Thank you for helping me.

[sample.com...]
should redirect to one of these:

[sample.com...] or [sample.com...]

I need help determining which version would be better.

I also need help with "other non-canonical versions".

The following was put in my file and I would like to know if it is incorrect.

*********************************

RewriteCond %{HTTP_HOST} ^sample\.net
RewriteRule ^(.*)$ [sample.net...] [R=permanent,L]

#Here should be rules for redirectig
RewriteRule ^index\.php$ [sample.net...] [R=301,L]
RewriteRule ^sampledirectory/index\.php$ [sample.net...] [R=301,L]
RewriteRule ^(.*)\.php/$ [coolchecks.net...] [R=301,L]

**************************

Thanks for helping me.

bluewaves



 
Msg#: 4547995 posted 8:52 pm on Feb 22, 2013 (gmt 0)

I don't know how to show the urls there without it truncating.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 10:51 pm on Feb 22, 2013 (gmt 0)

Read the post at the beginning of this Forum. (That is: Apache subforum, not WebmasterWorld as a whole.) It's Stickied so it stays at the top of the list.

If you need to name more than one domain you can use different tld's like dot com, dot org, dot net and so on. Even dot xyz if you like.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 10:58 pm on Feb 22, 2013 (gmt 0)

example.com is the hostname.

You need to also specify the page you want.

The root page is denoted by / after the hostname.

bluewaves



 
Msg#: 4547995 posted 11:29 pm on Feb 22, 2013 (gmt 0)

This is what I now have. I don't think this is doing what I want.

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

#Here should be rules for redirecting
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
RewriteRule ^exampledirectory/index\.php$ http://www.example.net/exampledirectory/ [R=301,L]
RewriteRule ^(.*)\.php/$ http://www.example.net/$1\.php [R=301,L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 12:06 am on Feb 23, 2013 (gmt 0)

The rules need to be in a slightly different order.

The non-www to www ruleset should be last. Rather than test for
^example\.com in the Condition, it should test for !^(www\.example\.com)?$ to ensure all non-canonical requests are redirected.

There's a couple of minor changes to make to the rules.

The rules for index pages each need a preceding
RewriteCond testing THE_REQUEST otherwise you end up with an infinite loop of redirecting and rewriting. As this question comes up every few days, it should be easy to find the code already posted here.

Add a blank line after each Rule to make the code easier to read.

Don't escape periods in the rule target.

Be consistent. Change
[R=permanent,L] into [R=301,L]
lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 3:41 am on Feb 23, 2013 (gmt 0)

I am not sure if the trailing slash should be the main target or the .com should be the main target.

Oh, now I understand what you meant.

The canonical form of your front page's name is
www.example.com/
with trailing slash. Most of the time you don't have to think about it because the user's browser itself-- not your server-- will add the slash when someone enters in a bare domain name. (Similarly the browser adds http:// at the front.)

Here it doesn't matter for a different reason. You will have a single generic redirect that says something like

RewriteCond %{HTTP_HOST} !{et cetera as already explained}
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

or example.net or www.example.co.uk or whatever your preferred form is.

This is your final RewriteRule. The second-to-last one is the "index.html" redirect. The target will look exactly the same as in the domain-name redirect; only the pattern will be different.

Note the condition in each case.

In the "index.html" redirect you need to ensure that the user asked for "index.html" by name, because mod_rewrite will also pick up requests that were handled internally-- in this case by mod_dir.

In the domain-name redirect you need to pick up all requests that did not use the right form of the name. ("Correct URLs are all alike. Incorrect URLs are each incorrect in their own way." --Tolstoy)

Detour to test site tells me that the RewriteCond in the index.html redirect can be replaced with the [NS] flag. But I am not 100% certain that this applies to all Apache versions everywhere, so don't take my unsupported word for it.

moxie



 
Msg#: 4547995 posted 6:44 pm on Feb 23, 2013 (gmt 0)

Alternatively you could use:

#REDIRECT NON-CANONICAL REQUESTS
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http ://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#REDIRECT INDEX REQUESTS
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index(\.(php|html?))?
RewriteRule ^(([^/]+/)*)index(\.(php|html?))?$ http://www.example.com/$1? [R=301,L]

Leave out the space after that http

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 8:28 pm on Feb 23, 2013 (gmt 0)

With that code, non-www index requests invoke an unwanted multiple step redirection chain.

It also fails to redirect requests for some non-canonical variants of the hostname.

moxie



 
Msg#: 4547995 posted 2:23 am on Feb 24, 2013 (gmt 0)

Ugh, I was told this was correct and have been using that for quite a while now; with at least no ranking issues anyways. Still though, if it's wrong it's wrong. Sorry about that as I'm still trying to learn all this stuff about htaccess. Now I have to go learn what I did wrong in there.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 2:48 am on Feb 24, 2013 (gmt 0)

^(([^/]+/)*)index(\.(php|html?))?$

Unless you have extensionless "index" pages-- which would be pretty senseless ;)--the pattern can be simplified to

^(([^/]+/)*)index\.(php|html?)?$

And you only need the question mark in html? if your site uses both forms. Remember that you only need to redirect index pages that actually exist; the others can take their 404 and lump it.

If you leave off the closing $ anchor you will concurrently get rid of visitors who are attempting some type of path-injection. This may or may not be desirable, depending on your real-life naming formats.

But the most important thing is to put the index redirect before the domain-name redirect. I mentioned in another thread that I recently discovered that requesting
wrong-form-of-domain-name/optionalblahblah/index.html
on one of my sites led to THREE consecutive redirects. (One of the extras was laziness on my part; the other was a typo. Ugh. All fixed now.)

bluewaves



 
Msg#: 4547995 posted 4:30 am on Feb 27, 2013 (gmt 0)

Lucy24 - I actually have this coding:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

to create extensionless urls. Is that a bad idea? I thought it was supposed to create seo friendly urls. This code will now produce http://www.example.com/try-me instead of http://www.example.com/try-me.php - But the bad thing is that if there is a hardcoded link with the .php extension it will show that page as well.

I prepared a list of redirects for the all my pages to go to the new pages, but with that coding above, it loops and fails to produce the page. What do I need to modify it to?

I don't want to be penalized for duplicate content.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 6:12 am on Feb 27, 2013 (gmt 0)

Now my head is starting to swim because I think there are three concurrent threads with overlapping questions :(

Yes, the code you quoted is one way to get the result you want. The piece you are missing is the redirect. It has to say-- in English-- "If a human user asks for blahblah.php by name, forcibly redirect them to the extensionless version."

It will be your

:: counting on fingers ::

second-to-last redirect: after the index.php redirect, before the final domain-name-canonicalization redirect.

The minimalist form goes

RewriteCond %{THE_REQUEST} \.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]

Note that the rule will never apply to "index.php" because that was dealt with in the immediately preceding redirect. (In general that is the second-to-last redirect. You've got an extra thanks to going extensionless.) The final ? is just for insurance: might as well throw out any explicit query string as long as you're at it. If your site doesn't use queries in any pages anywhere, you can leave it out.

If someone comes along after me and says that the Condition has to be longer and more detailed, pay attention.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 9:57 am on Feb 27, 2013 (gmt 0)

I actually have this coding <snip> to "create" extensionless urls.

Mod_rewrite cannot "create" anything. YOU "create" extensionless URLs by linking to them on the pages of your site. Mod_rewrite deals only with "requests" after a link is clicked.

Once a URL request is sent to the server, mod_rewrite rules can be used to alter the internal pointer ("rewrite" it) to point to a different internal location to that suggested by the URL request.

Link says
href="/page"

You click and browser asks for
GET /page HTTP/1.1

Without mod_rewrite, Apache would try to pull content from file at
/public_html/site_name/www/page

Since that doesn't exist, it would return "404 not found".

With the mod_rewrite rule present, the internal pointer is instead modified ("rewritten") to say
/public_html/site_name/www/page.php just before Apache attempts to fetch content.

This in no way stops a user requesting the page as
www.example.com/page.php with extension hence leaving you with a Duplicate Content problem to solve.

To fix that, you need another rule to redirect external requests for
/page.php to www.example.com/page and you do that with a RewriteRule for the redirect and a preceding RewriteCond testing THE_REQUEST to be sure this was an external request for /page.php and not an internal request as a result of a previous rewrite.

You should have 4 rulesets by now:

- redirect
index.html and index.php requests on any hostname to root ("/") or to folder ("/folder/") on www

- redirect other
.php requests on any hostname to extensionless on www

- redirect all requests for pages on
non-www to www

- rewrite extensionless requests to fetch content from
.php file

All 4 of the rules should be coded using
RewriteRule. The 3 redirects will each have a preceding RewriteCond (testing different things each time). The rewrite is a single line of code.

Mod_rewrite (when coded as a rewrite) doesn't make URLs for content. It works exactly backwards to that. It instead "translates" URL requests to find the content inside the server at a different location to that suggested by the path part of the incoming URL request.

When mod_rewrite is coded as a redirect, it tells the browser to make a new request for a different URL. It does this by returning the status code "301" along with the details of the new URL that the browser should request in place of the one it had just asked for.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 11:23 am on Feb 27, 2013 (gmt 0)

To clarify: When I said "second-to-last redirect" I meant specifically redirect. (It's called a RewriteRule but functionally it's a redirect.) Any rewrites will come after all redirects.

You can count on the fingers of each hand separately if it makes it easier ;) First hand has three fingers for the three redirects; second hand needs only one.

bluewaves



 
Msg#: 4547995 posted 3:55 pm on Feb 27, 2013 (gmt 0)

Lucy,

This:

RewriteCond %{THE_REQUEST} \.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]

Made it work.

I apologize for asking in two different threads and for having so many questions running.

I have a hard time understanding all of this so I also apologize for not getting everything that is being said to me.

If you wouldn't mind, I would like to show the following that is on my .htacess file with my question being, is anything not necessary and have I solved all the non-canonical issues.

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#Here should be rules for redirectig
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
RewriteRule ^directory/index\.php$ http://www.example.com/directory/ [R=301,L]
RewriteRule ^(.*)\.php/$ http://www.example.com/$1\.php [R=301,L]

#If file exists then do not process any other rules, just show it!
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule ^(.*)$ $1 [L]

#Drops the .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

#Redirect all external .php requests to the extensionless urls
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]

#Here is redirects for unexistant files

RewriteRule ^oldpage\.php http://www.example.com/new-page [R=301,L]
RewriteRule ^olderpage\.php http://www.example.com/newer-page [R=301,L]

Thank you so much for helping me.

bluewaves



 
Msg#: 4547995 posted 4:20 pm on Feb 27, 2013 (gmt 0)

Oh darn. My pagination isn't working now.

bluewaves



 
Msg#: 4547995 posted 4:22 pm on Feb 27, 2013 (gmt 0)

<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' class='lnknav'>

bluewaves



 
Msg#: 4547995 posted 4:52 pm on Feb 27, 2013 (gmt 0)

Temporarily I have fixed that by inserting the extensionless url where the bracketed text is. I hope there is an easier solution to this though. This would mean changing all pages.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 6:16 pm on Feb 27, 2013 (gmt 0)

In msg:4549484 your rules are in the wrong order.

You must list all redirects first and then all rewrites. This is important, otherwise some requests will invoke unwanted multiple step redirection chains.

In the list of redirects the rules must be ordered from most specific to most general.

In the list of rewrites the rules must be ordered from most specific to most general.


You have 6 blocks of code 1 - 2 - 3 - 4 - 5 - 6.

I think they should be ordered: 6 - 2 - 5 - 1 - 4 - 3 and you MUST add a preceding RewriteCond testing THE_REQUEST to every rule that deals with requests for index.php files or you will have an infinite loop.


The non-www/www rule can be improved by using a mosre specific pattern in the condition: !^(www\.example\.com)?$ in place of ^example\.com


The comment
#Drops the .php extension
is incorrect. That rule rewrites extensionless requests to find the correct internal php file.

As I said before in several threads in recent days "Mod_rewrite does not make URLs. It responds to URL requests after a link is clicked."

A rewrite is a fully internal process that maps an external URL request to an internal filepath. It is the Apache internal file pointer that is altered or "rewitten" in a rewrite, NOTHING happens to URLs in a rewrite.


The process you want here is to "redirect requests for .php URLs to the new extensionless URL" and to "rewrite incoming extensionless requests to fetch content from the matching php file".


Rewriting does not involve URLs! It involves the internal file pointer. Apache cannot alter the links on your pages.

bluewaves



 
Msg#: 4547995 posted 6:43 pm on Feb 27, 2013 (gmt 0)

Is this the correct order?

#Here is redirects for unexistant files
RewriteRule ^oldpage\.php http://www.example.com/new-page [R=301,L]
RewriteRule ^olderpage\.php http://www.example.com/newer-page [R=301,L]

#Here should be rules for redirectig
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
RewriteRule ^directory/index\.php$ http://www.example.com/directory/ [R=301,L]
RewriteRule ^(.*)\.php/$ http://www.example.com/$1\.php [R=301,L]

#Redirect all external .php requests to the extensionless urls
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#Drops the .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

#If file exists then do not process any other rules, just show it!
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule ^(.*)$ $1 [L]

*************************

"MUST add a preceding RewriteCond testing THE_REQUEST to every rule that deals with requests for index.php files or you will have an infinite loop. "

Does this:

RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

Need to be tested like you mentioned?

Will this do that?

RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

**********
I am trying to learn this. Thanks for helping me.

bluewaves



 
Msg#: 4547995 posted 6:59 pm on Feb 27, 2013 (gmt 0)

I think I have the right order here. Please indicate if something is incorrect:

RewriteRule ^oldfile\.php http://www.example.com/newfile.php [R=301,L]


#Here should be rules for redirectig
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
RewriteRule ^folder/index\.php$ http://www.example.com/folder/ [R=301,L]
RewriteRule ^(.*)\.php/$ http://www.example.com/$1\.php [R=301,L]



#Redirect all external .php requests to the extensionless urls
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]


RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#Rewrites extensionless requests to find the correct .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]


#If file exists then do not process any other rules, just show it!
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule ^(.*)$ $1 [L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 7:07 pm on Feb 27, 2013 (gmt 0)

To clarify the code further, add a blank line after every Rule (after every Rule, not after any of the Conditions), so you have 8 separate Rules (some of which also have one or more attached Conditions).

Yes you need a Condition to test THE_REQUEST before each of the rules that deals with index.php requests. Rules 2 and 3 each need this extra condition.

Rule 4 doesn't need .php in the rule target.
Rule 4 also needs a Condition the same as the Condition in Rule 5.

You've already caught on that the last rule, Rule 8, is pretty much redundant. :)

bluewaves



 
Msg#: 4547995 posted 7:29 pm on Feb 27, 2013 (gmt 0)

Is this better?

RewriteRule ^oldfile\.php http://www.example.com/newfile.php [R=301,L]


#Here should be rules for redirecting
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} foldername/index\.php$
RewriteRule ^foldername/index\.php$ http://www.example.com/folder/ [R=301,L]

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



#Redirect all external .php requests to the extensionless urls
RewriteCond %{THE_REQUEST} ([^.]+)\.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]


RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#Rewrites extensionless requests to find the correct .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

bluewaves



 
Msg#: 4547995 posted 10:58 pm on Feb 27, 2013 (gmt 0)

I am not sure what I am not getting, but when I enter the .php files in the url, shouldn't they redirect to the extensionless url? With what I have now, it is not happening.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4547995 posted 5:09 am on Feb 28, 2013 (gmt 0)

This package:

RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
RewriteRule ^directory/index\.php$ http://www.example.com/directory/ [R=301,L]


has two problems. One, each separate rule needs a Condition looking at %{THE_REQUEST}. Two, there don't need to be two separate rules. They collapse into

RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^(([^/]+/)*)index\.php http://www.example.com/$1 [R=301,L]


The * in Regular Expressions means "zero or more" while + means "one or more". So "zero occurrences of this pattern" will get you the top-level index page.

The RewriteCond should really be more exact; I've just given the minimal form that works.

In this

RewriteRule ^(.*)\.php/$ http://www.example.com/$1 [R=301,L]

Hasn't g1 already read you the riot act about using .* or .+ in non-final position? If he hasn't, it must be an oversight. Can we assume that your URLs do not contain literal periods anywhere? If so, it's a simple

^([^.]+)\.php

Here you want + rather than * because if someone comes in and asks for "www.example.com/.php" you don't really want to waste time on them do you? It wouldn't do any harm-- they'd be redirected to the front page unless you do the -f test-- but why bother?

Almost any rule that involves redirecting something ending in .php will benefit from the [NS] flag. In some cases it means mod_rewrite doesn't even have to detour to evaluate the Condition. Those nanoseconds add up :)


Oi! Preview! You squawk about everything else, why didn't you care that I left a whole [ code ] tag unclosed?

bluewaves



 
Msg#: 4547995 posted 3:48 pm on Feb 28, 2013 (gmt 0)

Is this better?

#Here should be rules for redirecting
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^(([^/]+/)*)index\.php http://www.example.com/$1 [R=301,L]




RewriteCond %{THE_REQUEST} ^([^.]+)\.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1 [R=301,L]


#Redirect all external .php requests to the extensionless urls
RewriteCond %{THE_REQUEST} ([^.]+)\.php
RewriteRule ^([^.]+)\.php http://www.example.com/$1? [R=301,L]



RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#Rewrites extensionless requests to find the correct .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4547995 posted 4:07 pm on Feb 28, 2013 (gmt 0)

Rules 2 and 3 are almost identical.

Rule 2 has a minor error and can be deleted as Rule 3 is more correct.

This 57 message thread spans 2 pages: 57 ( [1] 2 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved