Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining RewriteRules 1

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
US
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 " and now is "That is the /NTrak/ subdirectory is gone and everything is moved up one level.
So " becomes "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]
 
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 when a request 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$ [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$ [R=301,NC]

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

Bye, Olaf.
 
I now have this

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^NTrak/(.*)$ /$1 [R=301,NC]
RewriteRule ^[.+/](.*)\.htm$ [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.
 
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$ [R=301,NC]

And no other rewrite rule.

Bye, Olaf.
 
The NC flag is for case insensitive NC == NoCase

L is the Last rule flag.



Chris.

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

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

This does not do anything to url, including not stripping "NTrak
 
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$ [R=301,NC]

What does the Apache error log tell you?

Bye, Olaf.
 
maybe just /$1.php:

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

Bye, Olaf.
 
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 as For that you really need two rules, one just to remoe ntrak, the other to turn htm(l) to php.

Bye, Olaf.
 
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.
 
That's where you came from. But looking at some of my rewrite rules I think I have a better idea - introduce a RewriteBase:

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

Bye. Olaf.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top