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!
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.
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]
Related
I'm trying to rewrite my URLs with unique slug like Instagram or Facebook.
Example: facebook.com/joe
My URLs are like that: website.com/user.php?username=joe
I try this rule in .htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)$ user.php?username=$1 [L]
But it doesn't work, it redirects on /user.php.
It works if I use this rule:
RewriteEngine On
RewriteRule ^u/([^/]*)$ /user.php?username=$1 [L]
But result is website.com/u/joe and I prefer without u.
Any idea?
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?username=$1 [NC,L]
The .* in the pattern ^(.*)$ matches /anything and the parentheses help capture the anything part as a $1 variable used in the substitution URL as user.php?username=$1.
In case if you need multiple parameters you can simply add &%{QUERY_STRING} after $1 to separate and add them to the end of your query string.
for example : if you pass website.com/joe?age=31 the result will be website.com/user.php?username=joe&age=31.
Finally, the flag NC simply makes the rule non case-sensitive, so it matches /Joe or /JOE as well.
I would like to remove part of the url of my Joomla site and redirect to another url for example
www.example.com/xxx/yyy/zzz/FAQ
www.example.com/123/456/FAQ
to
www.example.com/FAQ
I'm very new to mod_rewrite in .htacesss
what RewriteRule can I write to achive this
thank you
Try this in root/htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /xx/yy/zz/([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /xx/yy/zz/$1 [NC,L]
And the same rule can be used to rewrite /123/456/faq to /123/456/FAq , just change the rewrite target and the pattern in first Rewritecond.
$1 is part of the regex in rewrite rule, its the part matched between (.+) ,it contains uri.
Thank you, after I tried I finally got it working
this is my solution
RewriteEngine On
RewriteRule ^.*/FAQ$ /FAQ [L,R=301]
by doing this I remove everything between the URL and FAQ
www.abc.com/SOMETHING/SOMETHING2/FAQ
and redirect the URL to
www.abc.com/FAQ
I also found a nice tool to test the mod_rewrite code
http://htaccess.madewithlove.be/
I have searched for hours for a tutorial to learn how I can rewrite an image path url containing a "?".
My images look like this:
<img src="/index.php?rex_resize=600w__/imageName.jpg" >
For the sake of caching all images, I must/want to remove this "?" from the string.
This is the rule I used:
RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]
This is all the content from my .htaccess with my rewriteRule inside:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]
RewriteRule ^sitemap\.xml$ index.php?rexseo_func=googlesitemap [NC,L]
RewriteRule ^robots\.txt$ index.php?rexseo_func=robots [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^redaxo/.*
RewriteCond %{REQUEST_URI} !^files/.*
RewriteCond %{REQUEST_URI} !^google(.*).html*
RewriteRule ^((.|\r|\n)+)/? index.php?params=$1 [L,NC]
</IfModule>
But since I've never learned proper RewriteRules, I get no match from this RewriteRule, and my knowledge about this is very limited.
For some help I'm thankful :)
You need to figure out a pattern to match against in your URL, to know you have to rewrite current request to your image script.
According to your post and commnents, let's say that the pattern is :
All URIs...
begining with files/
ending with a web image suffix (gif, jpg, png)
with something between those
Write this down into a regexp and your done :
/ : Begining of URI
files/ : our fake directory
(.*) : captures anything (will be stored in $1)
\. : a dot
(gif|jpg|jpeg|png) : file suffix (will be stored in $2)
$ : End of URI
RewriteRule :
RewriteRule ^/files\/(.*)\.(gif|jpg|jpeg|png)$ index.php?rex_resize=$1.$2 [NC,L]
Examples :
Incoming request:
/files/600w/imageName.jpg
Request after Mod_Rewrite:
/index.php?rex_resize=600w/imageName.jpg
Note that your example shows a redirection to /index.php (begining with a slash) which should not work when you use a .htaccess file (it only does when having rules in whost config)
Is it possible to make a rewrite rule to do this?
My php files all have first character uppercase and some may have a hyphen
like
Filename.php and File-Name.php
firstly
# Try this rewrite only if the file is not found
RewriteCond %{REQUEST_FILENAME} !-f
Then I want to make a rule for a request like
mysite.com/thing
mysite.com/Thing
mysite.com/THING
rewrites to the real file at
mysite.com/Thing.php
or (with the hyphens)
mysite.com/another-thing
mysite.com/Another-Thing
mysite.com/ANOTHER-THING
rewrites to the real file at
mysite.com/Another-Thing.php
Then if the file is STILL not found, give up and re-write the error to index.php i.e
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [R=301,L]
I can think of a way to do this in php though not sure if it could be done in rewrite e.g.
$url = str_replace('-', ' ', $url);
$url = ucwords(strtolower($url));
$url = str_replace(' ', '-', $url) . ".php";
Thank you.
You can add the following declaration to your apache or vhost config file:
RewriteMap lc int:tolower
And then the following rule either in the same file or in .htaccess files can operate on strings using this map function, for example
RewriteRule ^ - [E=LC_URI:${lc:%{REQUEST_URI}}]
And now you can use %{ENV:LC_URI} in your rewrite rules, or if you want to remove the leading /:
RewriteCond ${lc:%{REQUEST_URI}} ^/(.*)$
RewriteRule ^.* %1 [E=LC_FILE:%1]
etc.
This is my current .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/
RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]
Basically, it takes up to five "Friendly url folders" and assign the value to varibles and then, send those to my index.php page
IE: http://www.example.com/ford/focus/grey/
$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';
So far, so good.
Now, I need to integrate a 301 instruction (RegExp?) in that same .htaccess because initially, I had GET parameters like this :
IE: http://www.example.com/ford/focus/grey/?lang=fr
I need to get rid of all GET variables because Google sees it as duplicate content (even if I'm using the nofollow attribute on my languages links)
IE: http://i.want.to.keep/my/url/?AND_DUMP_GET_VARIABLES
http://www.example.com/ford/focus/grey/?lang=fr
http://www.example.com/ford/focus/grey/?lang=en
http://www.example.com/ford/focus/grey/?lang=sp
==> http://www.example.com/ford/focus/grey/
Logically, the instruction should be interpreted between the first and the second block but I just don't know where to start. Any hints?
THANKS!
As I understand you want to get rid of the QUERY STRING and redirect (301 Permanent Redirect) to the same URL but without QUERY STRING.
The rule below will redirect EVERY request that has query string:
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
1. The ? will do the magic -- will strip query string
2. You desperately need this line: RewriteCond %{ENV:REDIRECT_STATUS} ^$. The problem is that it may not work on your Apache setup and I cannot give you what exactly you need to make it work (it works fine on my vanilla Apache v2.2.17 on Windows).
After rewrite (internal redirect) occurred, it goes to next iteration and Apache starts matching all rules from the top again but for already rewritten URL. If we not add the above line, then mod_rewrite will apply the above rule to rewritten URL form and you will end up with all URLs get rewritten to /index.php with no parameters at all.
If the above will not work, then try the code below:
# do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
With help of # do not do anything for already existing files and folders rule, mod_rewrite will stop rewriting after URL will be rewritten to /index.php?p1=... format.
In case the above will not work at all (better -- in addition to the above -- I would suggest adding this anyway) use <link rel="canonical" href="FULL_PROPER_RUL"/> in your page:
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
http://www.google.com/support/webmasters/bin/answer.py?answer=139394