1

I've searched for other solutions to rewriting a URL using the .htaccess file at the root of my server, but the language is pretty confusing to me, and I can't seem to get it to work. How would I go about changing:

http://domain.com/share.php?media=059ogbuq70

to:

http://domain.com/059ogbuq70

I found code similar to this, and tried it, but it didn't seem to work:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^media=([0-9]+)$
RewriteRule ^$ share.php?media=$1 [QSA,L]

My PHP:

<?php
$media_id = $_GET['media'];
$url = file_get_contents("https://api.wistia.com/v1/medias/" . $media_id . ".json?api_password=my_key");
$json = json_decode($url, true);
$name = $json[name];
$origimg = $json['thumbnail'][url];
list($image, $size) = explode('?', $origimg);
$video = $json['assets'][5][url];
?>

I then echo the variables where I need them on my page.

Thank you!

4
  • I'm confused. You rewrite the URL to include a query string, but only if the query string exists. Do you want to redirect from the first to the second URL? Commented Dec 21, 2015 at 23:14
  • Basically I want the pretty url to only show to user, while the real url is what is used by the server/php Commented Dec 21, 2015 at 23:16
  • Do you also want to redirect "ugly" URLs to the corresponding "pretty" ones? Commented Dec 21, 2015 at 23:35
  • No, I just need domain.com/[some-string] to act like domain.com/share.php?media=[some-string] Commented Dec 21, 2015 at 23:45

2 Answers 2

1

To internally rewrite "pretty" URLs so that query strings are passed to PHP, I recommend the following .htaccess file:

RewriteEngine on
RewriteRule ^([^/]+)/?$ share.php?media=$1 [L]

The regex matches:
One or more characters that are not a backslash, optionally followed by a backslash.

Everything but the optional trailing slash are captured in $1 and rewritten as the "media" query string variable.

For example:

http://domain.com/059ogbuq70/
rewrites internally to
http://domain.com/share.php?media=059ogbuq70

Which means that:

$_GET['media'] = 059ogbuq70


You might find this mod_rewrite cheat sheet helpful for building your regex.
Also, you can test your rewrites here.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your answer, but this one is not rewriting the URL, and it's is interfering with my PHP (Getting variable from URI param media=).
Perhaps there is something else going on or I'm not understanding what you're aiming for. The rewrite is internal, so you won't see the URL change in the browser (redirect). It seems to be working here. It might help to edit your PHP code into your question.
I stand corrected, inputting a URL: domain.com/[some-string] does seem to be working just fine, but I'm having an issue with calling the $media_id variable on line 3 of my PHP. It was working just fine before the URL rewrite, and it works if I delete .htaccess
What is the value of $media_id with and without the .htaccess file?
Given your code, I don't see any way the $_GET variable would be set to "failed". "Failed" is one of the status values Wistia returns. Are you sure that's the value of $media_id? This is quickly deviating from the original question. Maybe it's best to ask a new question?
|
1

You need two rules for what you are trying to do. You can place this in the file called .htaccess

RewriteEngine on

#redirect all old URLs to the new rewritten URL
RewriteCond %{THE_REQUEST} ^GET\ /+share\.php\?media=([^&\ ]+)
RewriteRule ^ /%1? [R=302,L]

#rewrite folder path internally to share.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ share.php?media=$1 [QSA,L]

2 Comments

Thanks for your answer, but this one redirects to: http://vipheo.com/fs6e/vipheo/public/83s371n1yi%20HTTP/1.0 and gives me a 404. Any help?
@user1661677 yes sorry I corrected the the rule. Small error. Now the whole rule is complete. If you don't have a rule such as this, users will still be able to use the http://example.com/share.php?media=13e43 style URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.