Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Combining RewriteRules

Combining RewriteRules

Combining RewriteRules

Let me start by saying I know next to nothing about servers and htaccess.
I am trying to combine two major site changes by using htaccess.
This is so all old bookmarks and Search engine links don't break.

First off all the htm and html files have been changed to php.
Secondly the website was "www.Domain.Com/NTrak/..." and now is "www.Domain.Com/..."
That is the /NTrak/ subdirectory is gone and everything is moved up one level.
So "www.Domain.Com/NTrak/index.htm" becomes "www.Domain.Com/index.php"
I have tried the following and it remove the "NTrak" but does not change htm$ to php
If I use either one by itself they do what I want them to do, but they don't work in combination.
What am I dong wrong.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^NTrak/(.*)$ /$1 [R=301,NC]
RewriteRule ^(.+)\.htm$ $1.php [R=301,NC]

RE: Combining RewriteRules

Regex is no regularly used thing to me, but I did some recently.

The parenthesis in a regular expression defines the capture group coming out as $1 or $2, $3 if there is more than one.

As you want $1.php to be www.Domain.Com/file.php when a request www.Domain.Com/NTrak/file.htm comes in, you have two capture groups, the domain and the file name. But since the domain doesn't change, you finally still only need one capture group, just the file name without extension. Try

RewriteRule ^.+/(.*)\.htm$ www.Domain.Com/$1.php [R=301,NC]

In its parts it means: .+/ Anything from start to a slash, (.*) continuing with any file name (which becomes $1), \.htm$ ending in .htm

You might try variations, eg square brackets may help:
RewriteRule ^[.+/](.*)\.htm$ www.Domain.Com$1.php [R=301,NC]

Hope that helps. I'm sure you will get a fix, if I'm wrong.

Bye, Olaf.

RE: Combining RewriteRules

I now have this

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^NTrak/(.*)$ /$1 [R=301,NC]
RewriteRule ^[.+/](.*)\.htm$ http://www.domain.com$1.php [R=301,NC]

This strips the "NTrak" from the url, but does nothing with the htm to php part.
I think the problem is running two rules, but I don't know.
I think I need to combine the two rules into one.

RE: Combining RewriteRules

NC menas not continue and so the second rule is not executed.
You asked for one rule and I gave it, of course you have to delete the previous ones.

And I forgot a slash between .com and $1. You'll Need

RewriteRule ^[.+/](.*)\.htm$ http://www.domain.com/$1.php [R=301,NC]

And no other rewrite rule.

Bye, Olaf.

RE: Combining RewriteRules

The NC flag is for case insensitive NC == NoCase

L is the Last rule flag.


Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Combining RewriteRules

I now have this...

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^[.+/](.*)\.htm$ http://domain.com/$1.php [R=301,NC]

This does not do anything to url, including not stripping "NTrak"

RE: Combining RewriteRules

Thanks Chris,

I don't know, do we Need to escape a slash? It can't hurt, can it? Try without the square brackets, like this:

RewriteRule ^.+\/(.*)\.htm$ http://domain.com/$1.php [R=301,NC]

What does the Apache error log tell you?

Bye, Olaf.

RE: Combining RewriteRules

maybe just /$1.php:

RewriteRule ^.+\/(.*)\.htm$ /$1.php [R=301,NC]

Bye, Olaf.

RE: Combining RewriteRules

RewriteRule ^.+\/(.*)\.htm$ /$1.php [R=301,NC]

This is stripping all subdirectories.

so www.domain.com/NTrak/sub1/index.htm becomes www.domain.com/index.htm

getting close...

This is on a hosting service that I do not have access to apache logs.
One other thing, can HTML and HTM both be in this filter?

RE: Combining RewriteRules

OK, I made it simple, but if you need NTrak/sub1/.index.html to stay you simply need to add back NTrak

RewriteRule ^.+\/ntrak\/(.*)\.htm$ /$1.php [R=301,NC]

Now to get html and htm you add the l optional by adding a questionmark:
RewriteRule ^.+\/ntrak\/(.*)\.html?$ /$1.php [R=301,NC]

And actually you shouldn't need to escape a slash:
RewriteRule ^.+/ntrak/(.*)\.html?$ /$1.php [R=301,NC]

Looks a bit nicer. Since the rule is case not case sensitive, this also gets HTML, HTM, NTrak and all other cases.

What this won't do is rewrite www.domain.com/NTrak/sub1/index.htm as www.domain.com/sub1/index.php.
For that you really need two rules, one just to remoe ntrak, the other to turn htm(l) to php.

Bye, Olaf.

RE: Combining RewriteRules

Thanks, I will try this later today.
One question I still have.

Can I run two rules one after the other.
Would this be the best way?

These would say in all requests that have "/NTrak/" I want it gone.
This would be every url, be it for an htm, pdf, jpg, etc.

Then the next rule would just check htm & html and make them php.

As I said in the beginning I do not know how rules work in htaccess and I do thank you for the time you have already spent on this.

RE: Combining RewriteRules

That's where you came from. But looking at some of my rewrite rules I think I have a better idea - introduce a RewriteBase:

RewriteEngine on
RewriteBase /NTrak
RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC] 

Bye. Olaf.

RE: Combining RewriteRules

After working with the many examples that you gave me, I found that this works...

RewriteRule ^(.+)\.html?$ /$1.php [R=301,NC]
RewriteRule ^NTrak/(.+)$ /$1 [R=301,NC]

Thanks Olaf for your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close