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
Related
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]
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.
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.
im into SEO and friendly URL's and im trying to create a rule in my htacess file and i need help...
Basically, i have a list of alphabet letters. If the users selects one letter, the db will show all the lyrics that starts with that letter...
so if i click C, there will be a list of lyrics and the the first is 'Car and blues'
So, from this
htpp://www.website.com/lyrics.php?letter=C
i want to do this:
http://www.website.com/lyrics/C/
so far, this is what i have
RewriteRule ^lyrics/$ /lyrics.php?letter=$1 [L]
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Thanks
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Your rule as it stands is looking for exactly lyrics/ with no possibility of anything before or after it (as defined by the ^ and $).
Assuming you're using letters A-Z in only capitals, you can use this:
RewriteRule ^lyrics/([A-Z])/?$ /lyrics.php?letter=$1 [L]
This will look for a single capital letter after the lyrics/ and send that value to the rewrite URL and also match both cases of having a trailing / or not.
the rule should be smart enough to pick everything that comes after
'lyrics', in between the 2 slashes, and not what comes after...
I'd suggest you look into using regular expressions to format your url. See this link
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