Rewrite ucwords strtolower - .htaccess

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.

Related

.htaccess rewrite blank spaces with - ("%20" to "-")

I want to remove %20 on my link to - (dash),
My .htaccess is like that right now,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ icerik.php?name=$1 [QSA,L]
For example,
site.com/vision-and-mision,
site.com/do-re-mi-fa-so-la-si.
Actually I searched something but the informations were very specific and I'm confused
Thank you.
You can use the following in your /.htaccess file:
RewriteEngine On
# Replace whitespace with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all whitespace has been converted.
RewriteRule ^([^\s]*)\s(.*)$ $1-$2 [E=whitespace:yes,N]
# Then, once that is done, check if the whitespace variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:whitespace} yes
RewriteRule (.*) /$1 [R=302,L]
Then add your rules afterwards:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /icerik.php?name=$1 [QSA,L]
If this is working for you, and you would like to make the redirects cached by browsers and search engines, change 302 to 301.

Rewrite PHP query string?

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]

Redirecting all urls (including existing files) to the same page results in a 500 Internal Server Error

This is my current .htaccess file:
RewriteEngine on
# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R]
# everything
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
However, this doesn't work, it throws a 500 Internal Server Error
My previous .htaccess file looked like this:
RewriteEngine on
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)(/|\\)$ $1 [R]
# everything
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
And it worked, except for specific files. However, now I'd like the specific files to redirect into the handler as well. Is there a way to use RewriteRules without the RewriteConds?
Without RewriteCond for file check you can tweak your regex like this:
RewriteEngine on
RewriteBase /
# remove trailing slash for non directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R,L]
# every request not for handler.php
RewriteRule ^((?!handler\.php$).*)$ handler.php?url=$1 [L,QSA]
With the help of CBroe and Sumurai8, I was able to fix the problem on my own. The problem was not that you can't have RewriteRules without RewriteConds, but that if you rewrite every url into a single file, it will rewrite requests to that specific file to itself once more, creating an infinite loop.
The new .htaccess:
RewriteEngine on
# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R,L]
# everything
RewriteCond %{REQUEST_URI} !^/handler.php$
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]
Relevant resource: Request exceeded the limit of 10 internal redirects

Rewriting html-Image path containing "?" through mod_rewrite

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)

mod_reweite htaccess not work with a point in var

I creating a mod_rewrite rule like this
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME}\.php !-f
RewriteRule ^(.*)$ ./page.php?key=$1
it works! But I want to do this:
www.site.com/var1.var2.var3
(variables are separated by point) separated by - or _ no problems, but if I put the point does not work why?
I assume that you are trying a rule like this
RewriteRule ^(.*).(.*).(.*)$ ./page.php?key=$1&key2=$2&key3=$3
but you should remember to escape the dots which you want to interpret as periods. There are many ways to right this but here is a simple way if you are new to .htaccess files:
RewriteEngine On
RewriteBase /
# Skip existing files or directories
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
# Add default PHP extension when appropriate
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^.* $0.php [L]
# Pick up 1,2 and 3 parameter versions
RewriteRule ^(.*?)\.^(.*?)\.(.*)$ page.php?key=$1&key2=$2&key3=$3 [QSA,L]
RewriteRule ^(.*?)\.(.*?)$ page.php?key=$11&key2=$2 [QSA,L]
RewriteRule ^(.*?)\.$ page.php?key=$1 [QSA,L]
Note:
the first rule uses "^" as a pattern which always matches and the "-" means don't replace it.
the second uses $0 which means the entire match pattern
rule 3-5 match the 3,2,1 parameter options
the [L] (last) flag means skip the rest of the rules
you will need the QSA flag is you use other parameters.
The reason for splitting the existance check in to is that you need to handle files such as CSS and image (PNG/GIF/PNG) files as well as your application syntax.

Resources