Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

htaccess Rule to redirect and rewrite html links

Posted on 2010-11-11
10
Medium Priority
?
1,398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
10 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34116064
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 400 total points
ID: 34116085
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
ID: 34116097
Can you describe the conditions necessary for the 2nd URL to be redirected for codeigniter?
0
Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

 
LVL 40

Author Comment

by:Eoin OSullivan
ID: 34116622
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
 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 1600 total points
ID: 34125804
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 1600 total points
ID: 34128011
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 40

Author Comment

by:Eoin OSullivan
ID: 34130478
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
ID: 34130965
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

705 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