Htaccess Rewrite Rule with a PERIOD - .htaccess

I need a rule that when someone types
domain.com/finddomain.com it points to domain.com?q=finddomain.com
Very simple, yet the period in "finddomain.com" is causing my rule to fail.
My rule is:
RewriteRule ^([A-Za-z0-9.]+)(/)?$ index.php?q=$1
The "." screw it up.
Any help is much appreciated!

You have to escape the period with a backslash \. because the period stands for any character.
So your RegEx ^([A-Za-z0-9.]+)(/)?$ does actually match every string.. It should be ^([A-Za-z0-9\.]+)/?$ (or ^([A-Za-z0-9\.]+\.[a-zA-Z]+)/?$ to match only domains with a TLD).

try to escape the dot, since dot in regular expressions denote "anything"
cheers

Related

Redirect old URL to new URL by .htaccess

I want to change URL format from :
https://example.com/modules/news/article.php?storyid=224039
to :
https://example.com/news/224039
Any one can help to write true .htaccess codes?
thanks
Untested:
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+) /modules/news/article.php?storyid=$1 [NC,L]
The NC flag is for No Case, if you want case insensitivity. If not, remove this flag. The L is the Last flag, meaning it would be the last rule parsed in the given rewrite instance so further rewrites aren't used. This is a bit counterintuitive in the sense that Apache will re-read all the rules all over again from the beginning anyways after the rewrite to make sure it doesn't have to rewrite again, and is a gotcha for many people regarding infinite rewrite loops... Probably can also omit the L flag altogether, but is more expressive.
The RewriteEngine On can be omitted in Apache configurations that enable this in the httpd.conf file. It is best practice to put it on again before assuming the engine is on. The rewrite base / probably can be omitted, depends on how you write your RewriteRule. Finally the RewriteRule uses a regular expression on the left, the parenthesis stores the match, the brackets define a character list, 0-9 is the valid characters, could also use \d instead, the + means match 1 or more times. The expression on the right is what to replace it with. The leading slash can probably be omitted. Also note that due to the presence of a querystring on the right side, if a querystring was present on the left side, it will be discarded. If you want to merge query strings, use the QSA flag meaning querystring append, and then it will merge querystrings when adding your storyid. Finally the $1 means use the first match that was captured with parenthesis on the left.

^([^.?]+)$ and ^(\w+)$ in htaccess rewrite rule explanation

My problem is I'm confused between the dot(.) and the \w in htaccess some website says the dot is any character what so ever or any single arbitrary character and the slash w (\w) is matches any alphanumeric character and underscores. Can some explain this to me. I'm sorry for the question i just want to learn htaccess coding and can someone differentiate this to me ^(\w+)$ and ^([^.?]+)$. Im Hoping for your detailed explanation guys. Thank you !
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteRule ^(\w+) %{REQUEST_URI}.php [L]
I need some detailed explanation for this guys. Thank you
For the \w alphanumeric characters refer to characters from A-Z both upper and lowercase, and also the numbers 0-9. This also includes underscores as well. Hence it will match any letter, number or underscore. Whereas the dot will match literally any character whether it be a letter, number, symbol (?#$&!), a space. The dot will match any charater you can think of.
Therefore the difference between ^(\w+)$ and ^([^.?]+)$ is this:
^(\w+)$ will match:
apple
20apples
gr3at
1_boss
It will not match:
1 boss <-- Because of the space
$hady <-- Because of the dollar sign
happy feet <-- Because of the space
However ^([^.?]+)$ will match all the above statements, even if all you had were spaces, it would match

htaccess how to ask for ALL subdirectories

I need to say - this directory and anything after do this.
/thedirectory/* How would I write this?
Also I'm trying to figure out what exactly /|$ means. And what !^ means.
RewriteCond %{REQUEST_URI} !^/thepage(/|$)
Can someone help me with this? Researching trying to find the answer to my questions isn't coming up with answers.
This is about RewriteCond and regular expressions
To answer !^ first, it is two things ! (RewriteCond)
CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring:
1. You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used.
and ^ (regex)
Regex vocabulary
^ Called an anchor, matches the beginning of the string
/|$ is also a regular expression
/ matches a slash and has no special meaning
| is a special symbol and means or
$ is also an anchor and means end of string
So /|$ translates to: match a slash or match end of string.
Well I would suggest understanding what you are using before using it. The condition you have is opposite of what you are wanting. You should be researching regex characters rather than basic .htaccess help because then that will tell you what those mean.
The ! means not as in if not this page, then. So you need to remove that since you want to match on that page. ^ means start of the line basically. $ means the end and nothing else after that. Don't really need that in this case.
What you probably want is simply.
RewriteCond %{REQUEST_URI} ^/thediretory/? [NC]

Rewrite rule to remove all non alphanumeric symbols from a URI

I could not find the exactly the same question on SO. I hope someone can help me out with this.
Say, user entered http://www.example.com/abc#!def, and what I want to do is remove all symbols in the ${REQUEST_URI} portion, then do a redirect to http://www.example.com/abcdef. The problem is that these symbols can occur anywhere in the string, e.g. #ab!cdeg and abcdef#! should both redirect to abcdef.
If I'm correct, there is no string replace function for mod_rewrite, so this seems impossible to do, but am I correct?
You can capture specific parts of an URL with regular expressions in a RewriteCond
or RewriteRule, but not remove arbitrary characters.
Furthermore, you will never see the hash character '#' and everything after it in a URL, because it is used by the client to navigate to a specific part of the document.
Update using the next flag:
RewriteRule (.*)[^a-zA-Z](.*) $1$2 [N]
This rule removes all characters, which are not ^ alphabetic.

.htaccess prevent B flag from encoding + symbols or change occurences of %20 into +?

This line of my .htaccess file basically escapes and turns the first directory into a query string.
RewriteRule ^([^/]+)/?$ /a/?s=$1 [L,QSA,B]
I did this mainly to escape & symbols, but it escapes all non alphanumeric characters, including '+' symbols. I don't want to escape these ones so that urls are more clean.
eat%20a%20pizza
I want:
eat+a+pizza
Is it possible to somehow replace '%20' with '+' or prevent the B flag from encoding then?
Not sure if there's a way to be specific about how the B flag works, but you can change the %20 back to + with this:
RewriteRule ^(.*)%20(.*)$ /$1+$2 [NE,L]
You're probably going to need to find the right place to put that, as it needs to loop in order to get rid of all the %20's.

Resources