Solved

htaccess Rule to redirect and rewrite html links

Posted on 2010-11-11
10
1,385 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

27 Experts available now in Live!

Get 1:1 Help Now