Solved

htaccess Rule to redirect and rewrite html links

Posted on 2010-11-11
10
1,383 Views
Last Modified: 2012-05-10
We have a problem trying to create a htaccess rule

On the webserver we have the following URLs
http://www.domain.com/file.html/test/value/page.html
http://www.domain.com/file.html/test/value/page.html/value/two

We need a redirect URL to change the URL to find the last .html file in the path and redirect to the following
http://www.domain.com/page.html
http://www.domain.com/page.html/value/two

We also need to then Redirect the second URL for codeigniter
http://www.domain.com/index.php/controller/view/page/value/two

The problem is that the RegEx rules are difficult to compile and hard to test in htaccess.

Can someone help and provide the rules or a clear guidance as we are swamped with (!*./\$ variables.

Thank you



0
Comment
Question by:Eoin OSullivan
  • 3
  • 3
  • 2
10 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Firstly, this seems to work nicely (in a regex tester):

RewriteEngine on
RewriteRule ^file\.html.*?([^/]*\.html)((?:.(?!\.html))*)$ $1$2
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 100 total points
Comment Utility
If you can't depend on a fixed string "file.html" being in the URL, then this should work:

RewriteEngine on
RewriteRule ^.*?([^/]*\.html)((?:.(?!\.html))*)$ $1$2
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Can you describe the conditions necessary for the 2nd URL to be redirected for codeigniter?
0
 
LVL 39

Author Comment

by:Eoin OSullivan
Comment Utility
Thanks for contributions. To clarify ... The filename will vary but it will always be a .HTML file.
The issue is that the website has local HTML links but is running on codeigniter.
The 2nd rule is to then rewrite the HTML link into a codeigniter formatted URL.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 400 total points
Comment Utility
I'd use two rules to do this:

RewriteRule ^[a-zA-Z0-9]+\.html/test/value/([a-zA-Z0-9]+)\.html$ /$1.html
RewriteRule ^[a-zA-Z0-9]+\.html/test/value/([a-zA-Z0-9]+)\.html/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /index.php/index/index/$1/$2/$3

These are silent rewrites, but tack an [R=temporary] or [R=permanent] on the end of the lines for a 302 or 301 HTTP redirect.

The first rule spots the URLs without the /value/two pattern, and the second rule spots those with that pattern (and rewrites to the final "codeigniter" format you describe).

If I've misunderstood, examples of where this falls down would help.
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 400 total points
Comment Utility
In that case I'd use these rules:

RewriteRule ^(?:[a-zA-Z0-9]+\.html/test/value/)?([a-zA-Z0-9]+)\.html$ /$1.html
RewriteRule ^(?:[a-zA-Z0-9]+\.html/test/value/)?([a-zA-Z0-9]+)\.html/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /index.php/index/index/$1/$2/$3

This uses a (non-capturing) optional expression to make the first filename optional. This assumes that the unwanted filename will always be followed by "/test/value", but more work is needed if that's not always true.
0
 
LVL 39

Author Comment

by:Eoin OSullivan
Comment Utility
HackneyCab - the bit of the URL between the 2 HTML files will vary so we cannot assume it will be "test/value".  Am I right in thinking that finding the last .HTML file in a URL is not easy to do in htaccess.
0
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
Well, so long as there will only ever be two fake directories (test and value, for example) then you could replace /test/value/ with /[a-z]+/[a-z]+/ (or something similar if anything other than lowercase alphabetic characters can appear.

Matching variable patterns works fine using regex until you say "the path length/depth can be variable". Then you're in trouble, because regex doesn't offer a way to capture a variable number of sub-patterns.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now