Question

RewriteCond questions (.htaccess)

Asked by: baal32

Several .htaccess questions:

1) How can I add trailing slash in all cases unless a file extension is shown (or is it better to strip trailing slashes always?)

2) Do RewriteConds accumulate?  My problem is that I already have a rewriterule:
RewriteRule ^(.+)/from/(.+)/to/(.+)$ /$1/index.php?area=$1&from=$2&to=$3 [L,QSA]

But there are very old links to http://mydomain.com?area=aaa&from=bbb&to=ccc (before I restructured all my internal links for more SEO friendliness) and they're passing a good amount of PR.  So I want this link:
http://mydomain.com?area=aaa&from=bbb&to=ccc to 301 and pass PR to this
http://mydomain.com/areahere/from/A/to/B
which in turn will be parsed by the RewriteRule above

I don't have a good grasp of rewriteconds though - I have this
RewriteCond %{QUERY_STRING} from=(.+)&do=(.+)
RewriteRule ^(.*) http://www.mysite.com/$1/from/%1/to/%2 [R=301,L]

and further down this
RewriteRule ^(.+)/from/(.+)/to/(.+)$ /$1/index.php?area=$1&from=$2&to=$3 [L,QSA]

But when I pass in http://mydomain.com?area=aaa&from=bbb&to=ccc it seems like it gets 301ed properly, and then rewritten -  but then the rewritecond is reinvoked and it goes into a loop...

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-10-20 at 20:28:07ID21602668
Tags

rewritecond

,

htaccess

Topic

Apache Web Server

Participating Experts
1
Points
350
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. .htaccess RewriteRule and PHP problem
    Options SymlinksIfOwnerMatch RewriteEngine on RewriteCond %{REQUEST_URI} /[^\/]+/$ RewriteRule ([^\/]+)/ /index.php?product=$1 RewriteCond %{REQUEST_URI} /[^\/]+/[^\/]+/$ RewriteRule [^\/]+/([^\/]+)/ /index.php?product=$1&var=$2 This is my current .htaccess file, b...
  2. htaccess, rewritecond help
    Hello I need some help with htaccess. What i have currently: - if user types in main domain (mydomain.com) he sees main page - if user types in his subdomain (subdomain.mydomain.com) he sees his own page - if user types in his addon domain (addondomain.com) he sees his own ...
  3. Apache .htaccess rewriterule SEO friendly
    Hi, I would like to have the users redirected to a SEO friendly URL. Now : http://www.example.com/lyrics/index.php?p=show_lyric&titleID=123&lyric=Michael_Cretu&album=Invisible _Man_(1994)&title=Samurai SEO friendly : http://www.example.com/lyrics/Michael_Cr...
  4. htaccess rewriterule
    Hi, I have an htaccess rewrite rule that forwards any request ending in .html to my login script. This works great. However if somebody comes to my site as http://www.mysite.com then it dosnt forward to the login url because there is no /index.html. How can i fix this? opti...
  5. htaccess rewriterule not
    hi, i have the following htaccess rewrite rule that rewrites any url with xml in it. how can I change the rewrite rule so it only applies if the url is not sitemap.xml? thx RewriteRule xml /feed.php [L]
  6. RewriteRule  for .htaccess File
    Hi, I want to add a RewriteRule to the .htaccess file to redirect the following URL Address for example 999.example.com to http://www.example.com/index.php?page=list&uid=999 The number 999 should be a variable and it could any be any number. The idea is to have a sho...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: baal32Posted on 2005-10-20 at 20:29:45ID: 15130314

Quick edit - where it says http://mydomain.com/areahere/from/A/to/B it should actually say http://mydomain.com/aaa/from/A/to/B

 

by: caterham_wwwPosted on 2005-10-21 at 05:10:14ID: 15131510

> 1) How can I add trailing slash in all cases unless a file extension is shown (or is it better to strip trailing slashes always?)

RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule !^.+\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+)$ /$1/ [R=301,L]

> but then the rewritecond is reinvoked and it goes into a loop..
It might only loop with the other rule, if you're using &do insread of &to (/$1/index.php?area=$1&from=$2&to=$3), because otherwise the QUERY_STRING-Condition won't match. You should erease the query string in the substitution by adding a '?':

RewriteCond %{QUERY_STRING} from=(.+)&do=(.+)
RewriteRule ^(.*)$ http://www.mysite.com/$1/from/%1/to/%2? [R=301,L]

 

by: baal32Posted on 2005-10-21 at 08:34:36ID: 15133209

Sorry that was a typo:  it should be
RewriteCond %{QUERY_STRING} from=(.+)&to=(.+)

so now I have

RewriteCond %{QUERY_STRING} from=(.)&to=(.)
RewriteRule ^(.*) http://www.mysite.com/$1/from/%1/to/%2? [R=301,L]
RewriteRule ^(.+)/from/(.+)/to/(.+)$ /$1/index.php?area=$1&from=$2&to=$3 [L,QSA]

Same problem - (loops over and over again)

So even after a URL gets internally rewritten (like on line 3 above) it gets sent back *again* to mod_rewrite?  That's the only way I can see this happening

 

by: caterham_wwwPosted on 2005-10-21 at 08:42:09ID: 15133298

> Sorry that was a typo:

in this case, you've to use

RewriteCond %{THE_REQUEST} from=(.+)&do=(.+)\ HTTP
RewriteRule ^(.*)$ http://www.mysite.com/$1/from/%1/to/%2? [R=301,L]

instead.

 

by: caterham_wwwPosted on 2005-10-21 at 08:45:20ID: 15133328

> So even after a URL gets internally rewritten (like on line 3 above) it gets sent back *again* to mod_rewrite?

yes, this is the processing in .htaccess files. your URL /$1/index.php?area=$1&from=$2&to=$3 is reinjected into the processing again. Otherwise apache cannot check, if other Directives would apply to the new URL (i.e. some other RewriteRules)

 

by: baal32Posted on 2005-10-21 at 09:46:33ID: 15133778

Awesome!  Almost perfect - the only little thing now is that I end up with an extra /
http://domain.com/dining//from/A/to/B
                                     ^

Also I've increased the points in the hopes you'll answer this question (but if you don't that's ok, the question is yours anyway)
I'm still a little hazy on how RewriteConds get applied - for a rewriterule to take effect does it have to match *all* the rewriteconds above it, only if a rewritecond is immediately above it, or just the most recent rewritecond...?

# all go to www.
RewriteCond %{HTTP_HOST} ^domin.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L] # only rewrites if above rewritecond is true

# if not slash and not extension, add slash
RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule !^.+\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+)$ /$1/ [R=301,L] # only adds slash if above rewritecond is true

RewriteRule ^(.+)/sub/(.+)/$ /$1/index.php?area=$1&sub=$2 [L,QSA] #rewrites if match and disregards rewritecond above


This is a really simple question I just don't know how to phrase it :(  - do rewriteconds affect rewriterules far below them or only the immediate following rule or...

 

by: caterham_wwwPosted on 2005-10-21 at 10:47:48ID: 15134217

>  Awesome!  Almost perfect - the only little thing now is that I end up with an extra /

This might result from the fact, that you're filepath es empty (http://mydomain.com?area=aaa&from=bbb&to=ccc). Do you've any URLs like
http://mydomain.com/AnythingHereBeforeQueryString?area=aaa&from=bbb&to=ccc ? I removed the part from the RewriteRule (RewriteRule ^$ http://www.mysite.com/from/%1/to/%2? [R=301,L])

> I'm still a little hazy on how RewriteConds get applied

The processing is as follows:
Pattern RewriteRule
1st condition
2nd condition
... condition
(if everything [yes, all conditions above this Rule] is true:) substitution RewriteRule


RewriteCond a
RewriteRule a
RewriteRule b
as a result of the processing order, RewriteCond a can only apply to RewriteRule a and never to RewriteRule b. Buf if RewriteRule a is chained with RewriteRule b ([C]-Flag), RewriteRule b will only be executed, if RewriteRule a (=Pattern of Rule a and RewriteCond b) did match.

The reason why I used this and *not* this:
RewriteCond %{REQUEST_URI}  !^/.+\.[a-z]{2,4}$
RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule ^(.+)$ /$1/ [R=301,L]
is the processing: The RegEx .+ would match every request first and only in a 2nd step the URL is tested, if there's an extension (with the condition). This rule is slower, because every request would match at least the pattern of the rewriteRule.


# all go to www.
RewriteCond %{HTTP_HOST} ^domin.com
 # only rewrites if above rewritecond is true
RewriteRule ^(.*) http://www.mysite.com/$1 [R=301,L]

# the processing here is more complex:
# RewriteRule: no extension (if true ->)
# RewriteCond: no trailing slash (if true ->)
# RewriteRule: no substitution (-) ->
# RewriteRule: Flags - Chain with next Rule
RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule !^.+\.[a-z]{2,4}$ - [C]

# only if the ruleset above did mach (chained rules)
# RewriteRule: (.+) maches to every request with 1 to n chars
# RewriteRule: substitution
# RewriteRule: Flags -- force external redirection with statuscode 301 and last rule
RewriteRule ^(.+)$ /$1/ [R=301,L]

RewriteCond %{THE_REQUEST} from=(.+)&do=(.+)\ HTTP
# Rule will only match on domain.com?queryString, removed here $1
RewriteRule ^$ http://www.mysite.com/from/%1/to/%2? [R=301,L]

#rewrites if match and disregards rewritecond above
RewriteRule ^(.+)/sub/(.+)/$ /$1/index.php?area=$1&sub=$2 [L,QSA]

 

by: caterham_wwwPosted on 2005-10-21 at 10:49:07ID: 15134227

some typo:
as a result of the processing order, RewriteCond a can only apply to RewriteRule a and never to RewriteRule b. Buf if RewriteRule a is chained with RewriteRule b ([C]-Flag), RewriteRule b will only be executed, if RewriteRule a (=Pattern of Rule a and RewriteCond b) did match.

should be (RewriteCond a)

as a result of the processing order, RewriteCond a can only apply to RewriteRule a and never to RewriteRule b. Buf if RewriteRule a is chained with RewriteRule b ([C]-Flag), RewriteRule b will only be executed, if RewriteRule a (=Pattern of Rule a and RewriteCond a) did match.

 

by: baal32Posted on 2005-10-21 at 11:21:10ID: 15134454

I know I'm utterly dense - but just to totally clarify

RewriteCond %{THE_REQUEST} from=(.+)&do=(.+)\ HTTP                            <--  line X
# Rule will only match on domain.com?queryString, removed here $1
RewriteRule ^$ http://www.mysite.com/from/%1/to/%2? [R=301,L]

#rewrites if match and disregards rewritecond above
RewriteRule ^(.+)/sub/(.+)/$ /$1/index.php?area=$1&sub=$2 [L,QSA]      <-- my question, line Y

the reason line Y disregards the rewritecond at line X is becuase its seperated from that rewritecond by another rewriterule that doesn't have [c]?

Thanks for your patience caterham_www

 

by: caterham_wwwPosted on 2005-10-21 at 11:42:28ID: 15134622

RewriteCond %{THE_REQUEST} from=(.+)&do=(.+)\ HTTP                            <--  line 1
# Rule will only match on domain.com?queryString, removed here $1
RewriteRule ^$ http://www.mysite.com/from/%1/to/%2? [R=301,L]               <-- line 2

#rewrites if match and disregards rewritecond above
RewriteRule ^(.+)/sub/(.+)/$ /$1/index.php?area=$1&sub=$2 [L,QSA]      <--  line 3

yes, line 3 is seperated by an other rewriteRule (line 2) therefore the condition from line 1 cannot be tested with line 3. If you need the condition again for line 3, you'll need to repeat it directly above the Rule in line 3.

If you use the [C]-Flag for the rule in line 2, it doesn't mean "use Condition on line 1 also for the Rule on line 3". The meaning is "only test line 3, if the whole Ruleset did match". So, if the Ruleset (RewriteRule line 2+ Cond. from line 1) did match, line 3 is tested. The matching of the ruleset (ln. 1+2) will include of course the condition, but also the pattern of the RewroiteRule.

assuming a request of /abc/def

RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule ^mk - [C]

RewriteRule ^abc/(.*) /test/ [L]

this construct above (and with it line 3) will never match, because it fails on the pattern of line 2 (^mk = filepath begins with mk)

assuming again a request of /abc/def

RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule ^$ - [L]

RewriteCond %{REQUEST_URI} !^/.+/$
RewriteRule ^abc/(.*) /test/ [L]

This time the 2nd Ruleset would match (filepath begins with abc/(.*) and %{REQUEST_URI} doesn't have a trailing slash)

 

by: baal32Posted on 2005-10-21 at 14:05:59ID: 15135643

Awesome!  Thanks caterham_www !

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...