Solved

htaccess Rule to redirect and rewrite html links

Posted on 2010-11-11
10
1,389 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
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 100 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 39

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 400 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 400 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 39

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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