How do you write an htaccess RewriteRule to match hyphen characters? - .htaccess

I want to convert all php files in the imi directory to html
.htaccess rules:
RewriteRule ^imi/([0-9|a-z]*).html$ imi/$1.php
The following rewrite correctly:
Imi/abc.php to imi/abc.html
Imi/6s.php to imi/6s.html
Imi/hi4.php to imi/hi4.html
Imi/5.php to imi/5.html
Imi/ho12bc.php to imi/ho12bc.html
Imi/miss.php to imi/miss.html
Whereas these don't:
Imi/abc-1.php to imi/abc-1.html
Imi/6s-my.php to imi/6s-my.html
Imi/hi4-oe-5.php to imi/hi4-oe-5.html
Imi/5-lb.php to imi/5-lb.html
Imi/ho12bc-123-us.php to imi/ho12bc-123-us.html
Imi/miss-sir.php to imi/miss-sir.html
Why do the addresses with - not work

Your rewrite rule should include the hyphen as the first character in the matching pattern. Also, you shouldn't need the pipe character in there. Try this:
RewriteRule ^imi/([-0-9a-z]*).html$ imi/$1.php

Related

mod_rewrite: Check if query contains soft hyphen and remove it

In my http logs I see:
"GET /category/f%C2%ADile-to-download/ HTTP/1.1" 301
instead of "GET /category/file-to-download/ HTTP/1.1" 200
I discovered that %C2%AD is a soft hyphen (invisible symbol).
I need to check if a query to Apache contains a soft hypen and if it does to remove it. Any suggestions on the best method to locate soft hyphen and remove it?
I made some tests with RewriteRule, but got stuck.
Thanks!
As I understand it, mod_rewrite uses un-escaped characters, so in order for you to correctly match the soft-hyphen and then remove it, you would need to edit and save your .htaccess file in UTF-8 encoding (most modern editors will do this).
You will then need to enter a soft-hyphen into your rule. The following will (should!?) remove a single soft-hyphen from your input, but as mentioned it relies on the file being in UTF-8 format:
RewriteRule ([^-]*)-([^-]*) $1$2
Note that you would need to replace the - with the actual UTF-8 dash.
Perhaps an easier option would be this:
RewriteRule ([^\xc2\xad]*)\xc2\xad([^\xc2\xad]*) $1$2 [N]
It uses the specific UTF-8 code you're seeing to remove it from the string. The [N] should rerun all the rewrite rules, which will remove any remaining soft-hyphens.
Thanks #icabod
Currently I got this rule working in my case:
RewriteCond %{REQUEST_URI} \xc2\xad [NC]
RewriteRule ([^\xc2\xad]*)[\xc2\xad]+([^\xc2\xad]*) /$1$2 [N,R=301,L,NC]
.htaccess should be in UTF-8 format as mentioned above.
R=301 - redirect with HTTP code 301
NC - case insensitive
But it doesn't work with two soft hyphens in the different places of the URL like this:
/category/f%C2%ADile-to-d%C2%ADownload/

.htaccess basic mod rewrite

I am only just starting to learn how to rewrite urls with the .htaccess file.
How would I change:
http://www.url.net/games/game_one.php
into this:
http://www.url.net/games/game-one/
This is what I have been trying
RewriteRule ^/games/game-one.php ^/games/game-one/ [NC,L]
If you want people to use /games/game-one/ explicitly, you have to rewrite so that it requests /game/game-one.php. So the opposite way around than you have it in your question.
RewriteEngine On
RewriteRule ^games/game-one/$ /games/game-one.php
If you want to rewrite other URL's too, then you'd need to use a technique similar to the prior answer.
Try this:
RewriteRule ^(/games/game-one)\.php $1/
What that says is match anything starting with /games/game-one and remember the first part of that match, then replace it with the first part (capturing group in regex speak), and a slash character. Note that to match a period character you must precede it with a \ since . is a special character that means "any character" (at least if you care to avoid matching any character).

How to write this .htaccess rewrite rule

I am setting up a MVC style routing system using mod rewrite within an .htaccess file (and some php parsing too.)
I need to be able to direct different URLs to different php files that will be used as controllers. (index.php, admin.php, etc...)
I have found and edited a rewrite rule that does this well by looking at the first word after the first slash:
RewriteCond %{REQUEST_URI} ^/stats(.*)
RewriteRule ^(.*)$ /hello.php/$1 [L]
However, my problem is I want it to rewrite based on the 2nd word, not the first. I want the first word to be a username. So I want this:
http://www.samplesite.com/username/admin to redirect to admin.php
instead of:
http://www.samplesite.com/admin
I think I just need to edit the rewrite rule slightly with a 'anything can be here' type variable, but I'm unsure how to do that.
I guess you can prefix [^/]+/ to match and ignore that username/
RewriteCond %{REQUEST_URI} ^/[^/]+/stats(.*)
RewriteRule ^[^/]+/(.*)$ /hello.php/$1 [L]
then http://www.samplesite.com/username/statsadmin will be redirecte to http://www.samplesite.com/hello.php/statsadmin (or so, I do not know the .htaccess file)
To answer your question, "an anything can be here type variable" would be something like a full-stop . - it means "any character". Also the asterisk * means "zero or more of the preceding character or parenthesized grouped characters".
But I don't think you need that...If your matching url will always end in "admin" then you can use the dollar sign $ to match the end of the string.
Rewrit­eRule admin$ admin.php [R,NC,L]
Rewrites www.anything.at/all/that/ends/in/admin to www.anything.at/admin.php

.htaccess url rewrite issue with dot

I want to create my url structure like::
facebook i.e. facebook.com/?pageid=122
For which I am using htaccess mod rewrite as:
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#]*)$ /$1.php?$2 [QSA,L]
so I may translate pages like site.com/home/?pageid=22 into site.com/home.php?pageid=22
The code above works fine except that if I try to add dot like
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The .htaccess breaks. I need dot so I may pass emails too i.e.
site.com/home/?email=sohaib.dmc#gmail.com
Please help
Try to remove the backslash before the dot. Since it's not considered as a special character inside brackets in a POSIX regular expression.
You need scape the question mark because it's a special character:
RewriteRule ^([a-zA-Z_\-]+)/\?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches both colour and color.
http://www.regular-expressions.info/optional.html

htaccess rewrite problem

RewriteRule ^a/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)$
/var/www/vhosts/mydomin.com/httpdocs/search.php?searchtext=$1&locationtext=$2&page=$3
[QSA]
I want to pass http://www.mydomain.com/searchtext=jobs&locationtext=A.G.sBosRoad&page=1,
but I'm getting an error. I'm guessing this error is due to the . characters. What modification is needed in htaccess to allow read . characters?
Your regex for locationpart doesn't accept dots, as you say. Change the character class to include \.:
RewriteRule ^a/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_\.-]+)/([0-9]+)$ /var/www/vhosts/mydomin.com/httpdocs/search.php?searchtext=$1&locationtext=$2&page=$3 [QSA]
or, more generally, if your script doesn't have problems with it:
RewriteRule ^a/([^/]*)/([^/]*)/(\d+)$ /var/www/vhosts/mydomin.com/httpdocs/search.php?searchtext=$1&locationtext=$2&page=$3 [QSA]
That will accept any string without slashes for locationtext and searchtext, even the empty string, and still redirect to your search script.
.htcaccess only accepts files that end in a proper extension such as .html or .php.

Resources