htaccess how to ask for ALL subdirectories - .htaccess

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]

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.

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.

htaccess redirect/rewrite help

I want to redirect domain.com/1/file.php to domain.com/2/file.php and domain.com/1/pic.jpg to domain.com/2/pic.jpg etc. I want to redirect the subdirectory while leaving to end file in tact. I'm finding it hard to look for a tutorial as I can't describe my problem easily in google. Could someone show me how to do this or direct me to a tutorial about it.
Thanks
RewriteEngine On
RewriteRule ^([0-9])/([a-zA-Z]+).php$ 2/$2.php
Using mod_rewrite...You can do the same thing for your images as well. Essentially the regex expression will collect the value for a backreference. so $1 and $2 since i put 2 regex expressions in there.
Of course, if you need a different match instead of domain.com/1/... you will need to have a regex pattern that matches (but you can just google the appropriate regex expressions you may need but i took your example literally)
Try following for redirect. Replace 1 and 2 with respective subdirectories.
RewriteEngine On
RewriteRule ^1/(.*\.)(jpg|php) /2/$1$2 [L,NC,R=302]

Htaccess Rewrite Rule with a PERIOD

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

Resources