htaccess to redirect link and preg_match to load iframe contents OR ??

Hello,

We have specific links on a domain that need to be redirected to a single php page, which will then parse the URL to generate the src attribute of an iframe.

An example of a link is http://domain.com/preview/123456789/0987654321/filename-goes-here.ext

My thoughts are to use htaccess to redirect ALL requests to http://domain.com/preview/ to a single file containing the iframe -- suggestions are welcomed ! :)

That single file would then need to parse the URL to create the src attribute for the iframe.

So, using the example link above, the iframes src would be:
http://anotherdomain.com/sub/123456789/0987654321/filename-goes-here.ext

So, any request for links beginning with http://domain.com/preview/
will open one specific file via htaccess rule
and that file needs to parse the URL - AFTER the /preview/ piece to build its src attribute for the iframe within it.

As a front end/UI guy.. this is racking my brain.. and any help on the htaccess redirect and best way to parse the URL within the single file is greatly appreciated. Also, of note, I would prefer a PHP solution to this although I'm open to considering javascript solutions for the URL handling within the single file.

Thanks!
LVL 8
Jesse MatlockUX EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tpraveenrajCommented:
This code'll help u to redirect through .htaccess.
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^preview/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).ext$ http://anotherdomain.com/sub/$1/$2/$3.ext

Open in new window

0
Jesse MatlockUX EngineerAuthor Commented:
That is great.. but I actually need the single page that opens to just be http://domain.com/single-file.php and the iframe WITHIN it to have that exact regex you described above.. so, all links starting with http://domain.com/preview will direct to this single.php file.. but this single.php file must then process the complete URL (possibly using preg_match or ??) to generate the src attribute for the iframe.. make sense?
0
tpraveenrajCommented:
Then, the following .htaccess code will redirect to single-file page and the regex will be sent through get method.

Look at the code
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^preview/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).ext$ single-file.php?x=$1&y=$2&z=$3

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Jesse MatlockUX EngineerAuthor Commented:
I'm using nearly as you have entered it above, just changed the name of the /preview/ 'trigger' to something else.. but, it is not working.. I get a 404 error.

Here is the adjusted rule that I'm using:

^friends/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ connect/new.php?x=$1&y=$2&z=$3

Any thoughts as to why its not working? It looks like this is exactly what I need.. I already have the code in the new.php to handle the GET variables.. but, we're never getting to that file to process them. Thanks!
0
Jesse MatlockUX EngineerAuthor Commented:
Also, just for reference... I tested the new.php file with the query string URL and it works great.. so, it seems only in the rewrite that its failing..
0
tpraveenrajCommented:
Is ur new.php file is under http://domain.com/connect/ ?

Since, ti should work, i'd cross checked the code, it's correct
0
Jesse MatlockUX EngineerAuthor Commented:
Yes.. the structure is:
http://domain.com/friends/123456789/09876543/example-file.html is the link TO the new.php page

the new.php page is located here:
http://domain.com/connect/new.php

If I feed the new.php variables it works great.. This:
http://domain.com/connect/new.php?x=123&y=456&z=789 load the correct page in the new.php file
0
Jesse MatlockUX EngineerAuthor Commented:
I'm definitely not a expert on the more complex workings of htaccess files.
However, doing a bit of research and finding this error it appears the coding
presented in this htaccess file places the request in a loop.

any suggestions?
0
Brad HoweDevOps ManagerCommented:
Hi,

Here are a couple suggestions.

1. In your httpd.conf file add the following under your <virtualhost> settings.
        RewriteEngine On
        RewriteLog /<some folder location with the correct permissions>/rewrite.log
        RewriteLogLevel 9
        RewriteMap escape int:escape


2. In your directory "friends" create the following .htaccess file.
        RewriteEngine On

   
0
Jesse MatlockUX EngineerAuthor Commented:
rewriting is working as I have other URLs being rewritten in the same directory.. any other thoughts ?? Our sysadmin said it was something related to the code.. also, when I input it into the htacces file, the second portion of the rewrite rule is green.. where the others in the file are yellow.. it usually only does that with a syntax error (textmate).. but I don't see what it is..
0
Brad HoweDevOps ManagerCommented:
Opps forgot the rule when i was TABBING.

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/new.php?x=$1&y=$2&z=$3

http://mydomain.com/somefolder/123/456/example.html 
is redirected to.
http://mydomain.com/connect/new.php?x=123&y=456&z=example.html

This should help you at least get logging to see what errors you are getting should you have conflicting rules.

As per the rule above, it works fine. I haev just tested it in my dev env. I don't think it what you are looking for but it should help you out.

Cheers,
Hades666


0
Jesse MatlockUX EngineerAuthor Commented:
excellent.. giving it a try now
0
Jesse MatlockUX EngineerAuthor Commented:
One thing I forgot to mention is that I need to include that initial directory so we can redirect to one of two files..

eg:
http://mydomain.com/SOME/123/456/example.html
is redirected to.
http://mydomain.com/connect/NEW.php?x=123&y=456&z=example.html

and

http://mydomain.com/ANOTHER/123/456/example.html
is redirected to.
http://mydomain.com/connect/OLD.php?x=123&y=456&z=example.html

0
Brad HoweDevOps ManagerCommented:
Same rule then just add it to the Regex

RewriteRule ^SOME/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/new.php?x=$1&y=$2&z=$3

RewriteRule ^ANOTHER/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/OLD.php?x=$1&y=$2&z=$3

Cheers,
Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
I do that.. but it doesn't work.. :( - essentially the same rewrite as @tpraveenraj has .. it just loops.
0
Brad HoweDevOps ManagerCommented:
That shouldn't happen...  Testing now, one sec. Hades666
0
Brad HoweDevOps ManagerCommented:
K, This may be a stupid question but were are you saving your .htaccess file?

In the root? in the /SOME dir? in the /ANOTHER dir? The pattern changes depending on where you place it.

In this case i have the following .htaccess in my document root and it rewrite just fine.

             RewriteEngine On

             #catches http://mydomain.com/SOME/123/456/example.html
             RewriteRule ^SOME/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/new.php?x=$1&y=$2&z=$3 [R=301,L]

             #catches http://mydomain.com/ANOTHER/123/456/example.html
             RewriteRule ^ANOTHER/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/OLD.php?x=$1&y=$2&z=$3  [R=301,L]



You could also just put it in each DIRECTORY if you have conflicting rules. This is way i suggested adding the rewrite log with verbose logging to see what is actually happening.

1 in the /SOME directory
   and the .htaccess file should contain the following
             RewriteEngine On
             RewriteRule ^SOME/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/new.php?x=$1&y=$2&z=$3 [R=301,L]


1 in the /ANOTHER directory
   and the .htaccess file should contain the following
             RewriteEngine On
             RewriteRule ^ANOTHER/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/OLD.php?x=$1&y=$2&z=$3 [R=301,L]


Let me know,
Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
I've placed it in the docroot.. and thats the think.. the 'SOME' directory doesn't exist.. we just need a way to 'switch' between using one of two single files to display the iframe..

So,
http://domain.com/SOME ........... would load /connect/new.php
AND
http://domain.com/ANOTHER........ would load /connect/new.php

But there is NO SOME or ANOTHER directories.. these will just be links within the main site..

Also, of note.. this is a Wordpress blog..
0
Jesse MatlockUX EngineerAuthor Commented:
oops.. that second line (the after AND) should result in loading /connect/OLD.php ;)
0
Brad HoweDevOps ManagerCommented:
Hi,

The some and another directories don't need to exist. That is the reason for mod_rewrite? For example I have the exact rules inplace in the devenv and they load just fine. I can even change some/another to dir1/dir2 (which don't exist either) and they are redirected.


Please turn on Rewritelogging to get the details of what is going on in the engine.

Cheers,
Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
Request sysadmin to turn it on now.. where would I look for the log for this.. I've not used this before.
Thanks!
0
Jesse MatlockUX EngineerAuthor Commented:
Logging is not on yet,.. but I am almost certain the base wordpress htaccess rules are what is causing the conflict, these are the existing rules in the root of the site:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
0
Brad HoweDevOps ManagerCommented:
OH well that changes things... lol

Place the two rules at the top of "%{REQUEST_FILENAME} !-f"

             #catches http://mydomain.com/SOME/123/456/example.html
             RewriteRule ^SOME/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/new.php?x=$1&y=$2&z=$3 [R=301,L]

             #catches http://mydomain.com/ANOTHER/123/456/example.html
             RewriteRule ^ANOTHER/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+).html$ /connect/OLD.php?x=$1&y=$2&z=$3  [R=301,L]

Those are coditions to send ot index.php if a file or folder is not found.

Hades666
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jesse MatlockUX EngineerAuthor Commented:
ok.. that and adding \. within the last group worked ! Now, the URL changes though.. is there a way to keep the initial URL in the address bar ??

So, http://mydomain.com/SOME/123/456/example.html
becomes
http://mydomain.com/connect/new.php?x=123&y=456&z=example.html
0
Brad HoweDevOps ManagerCommented:
To do that, you would need to do it programatically and i don't think that is possible.

You want users to enter
    http://mydomain.com/SOME/123/456/example.html
which apache interprets as
    http://mydomain.com/connect/new.php?x=123&y=456&z=example.html
but the browser shows
    http://mydomain.com/SOME/123/456/example.html

This will cause cyclic loops.

mod_rewrite is one format to another unfortunately.

Cheers,
Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
ah.. ok.. so there is no way to 'hide' or make more presentable those variables that are passed in the link..?
I've seen other sites do this,.. but I'm not sure how they accomplished it.. just liked the clean URL as the result lol
0
Brad HoweDevOps ManagerCommented:
the issue is that you are passing a clean URL, desconstructing it to make it dirty :) and then want to present it as the clean URL.

Mod_rewrite can either take a

clean URL and make it dirty
mydomain.com/parts/partnumber/ to mydomain.com/parts.php?name=partnumber
OR
mydomain.com/parts.php?name=partnumber to mydomain.com/parts/partnumber/

But they cannot take a clean, make dirty and make it the original....  

Sorry,
Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
That makes perfect sense..

so if mod_rewrite cannot do it.. how is it that some sites achieve this?

I guess one way would be a self-hosted link shortening script.. would that do it ??
0
Brad HoweDevOps ManagerCommented:
I have no idea at the moment. Hades666
0
Jesse MatlockUX EngineerAuthor Commented:
Fair enough.. well, you have gotten me as far as this question asked.. and I greatly appreciate it!
Thanks!
0
Jesse MatlockUX EngineerAuthor Commented:
Very well done.. thanks again
0
Brad HoweDevOps ManagerCommented:
If you open another question in the PHP forums, I sure they (And I) would be able to see what we could come up with.

Let them know about the mod_rewrite you implemented too :)

Cheers,
Hades666
0
Brad HoweDevOps ManagerCommented:
LOL, That was PHP... I answered it thorugh Apache :P.
Hades666
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.