Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

htaccess Rule to redirect and rewrite html links

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
Eoin OSullivan
Asked:
Eoin OSullivan
  • 3
  • 3
  • 2
3 Solutions
 
Terry WoodsIT GuruCommented:
Firstly, this seems to work nicely (in a regex tester):

RewriteEngine on
RewriteRule ^file\.html.*?([^/]*\.html)((?:.(?!\.html))*)$ $1$2
0
 
Terry WoodsIT GuruCommented:
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
 
Terry WoodsIT GuruCommented:
Can you describe the conditions necessary for the 2nd URL to be redirected for codeigniter?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Eoin OSullivanConsultantAuthor Commented:
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
 
HackneyCabCommented:
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
 
HackneyCabCommented:
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
 
Eoin OSullivanConsultantAuthor Commented:
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
 
HackneyCabCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now