htaccess redirect starting URL expression to blog page - regex

I want to redirect certain URLs starting with an expression. For ex
I want to redirect:
www.example.com/%2FE (www.example.com/%2FExxxxxxxx) to my blog page in my .htaccess file.
I can redirect www.example.com/2FExxxxx but I am not able to target the %.
The xxxx... I have used in the URL is to represent any expression after %2FE.
This is my code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %2FE /blog [R=301,L]
<IfModule>
Can anyone here help me?

By default Apache rejects (with a server generated 404) any URL that contains an encoded slash (%2F) in the URL-path part of the URL. This occurs before the request is processed by .htaccess. (This is considered a security feature.)
To specifically permit encoded slashes, there is the AllowEncodedSlashes directive (default value is Off). But this can only be set in a server or virtualhost context. It cannot be set in .htaccess. To permit encoded slashes, AllowEncodedSlashes can be set to either On or NoDecode (preferable).
For example:
# In a server / virtualhost context (not .htaccess)
AllowEncodedSlashes NoDecode
Then, once the above has been implemented in the server config and the webserver restarted, you can proceed to match the slash using mod_rewrite in .htaccess...
RewriteRule %2FE /blog [R=301,L]
Ordinarily, the RewriteRule pattern matches against the %-decoded URL-path. However, if the NoDecode option has been set then the encoded slash (%2F) is not decoded. So the above "should" work (except that the pattern is not anchored, so potentially matches too much).
But note that multiple (decoded) slashes are reduced in the URL-path that is matched by the RewriteRule pattern. So matching multiple-contiguous slashes here is not possible.
I would instead match against the THE_REQUEST server variable, which is as per the original request and always remains %-encoded (if that is how the request has been made). And multiple slashes are preserved. Note that THE_REQUEST contains the first line of the HTTP request headers, not just the URL-path.
For example:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/%2FE [NC]
RewriteRule . /blog [R=301,L]
You should not use the <IfModule> wrapper here.

Related

redirect all requests starting with 0x to index.html

My website will be like example.com/0xETHEREUMADDRESS.
So i want to redirect all those request starting with 0x to INDEX.HTML and index.html has already the code to the rest of work.
I need .htaccess code to redirect all starting with 0x to index.html.
Here is my tried .htaccess rules file.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule (0x*)$ /index.html [L,R=302]
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule (0x*)$ /index.html [L,R=302]
The regex (0x*)$ matches URLs that end-with 0, 0x, 0xx, 0xxx etc. It does not match URLs that start-with 0x, so this will not match the desired URL. The * character is a regex quantifier that repeats the preceding token 0 or more times. There is also no need for the capturing group (surrounding parentheses).
If the rule only matches URLs that start with Ox then the condition that checks the URL is not /index.html is therefore redundant.
The following will do what you are asking:
RewriteRule ^0x /index.html [R=302,L]
The ^ is the start-of-string anchor, so the requested URL must start with 0x. Note that the URL-path matched by the RewriteRule pattern does not start with a slash.
However, I'd question whether you really want to "redirect" the user? (As in an external HTTP redirect - which is what this is.) Redirecting will lose the original URL and expose /index.html to your users.
Internal "rewrite" instead
If, however, you wish to internally rewrite the request instead so that index.html can analyse the requested URL (as you say, "index.html has already the code to the rest of work") and keep /0xETHEREUMADDRESS in the browser's address bar then remove the R=302 flag and the slash prefix on the substitution string. For example:
RewriteRule ^0x index.html [L]
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html#regex

HTACCESS : Redirect (301) thousands of Url's containing directories to simple url's

I need to convert with HTACCESS method tons of URL's allready produced (and already indexed...) by Wordpress for articles containing folders/subfolders to simples URL's without any folder/subfolder name.
Example:
FROM https://www.website.com/Animals/Cats/mycat.html TO https://www.website.com/mycat.html
FROM https://www.website.com/Animals/Dogs/mydog.html TO https://www.website.com/mydog.html
FROM https://www.website.com/Countries/France/bordeaux.html TO https://www.website.com/bordeaux.html
etc...
I already changed permalinks options in Wordpress config. So, now URL's produced are in the good format (Ex: https://www.website.com/bordeaux.html) without any folder name.
My problem is to redirect all OLD Url's to this new format to prevent 404 and preserve the rank.
If tryed to add in my .htacess this line :
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
I egally tryed RedirectMatch 301 (.*)\.html$ method and it's the same. I'm going crazy with this.
What am i doing wrong and could you help me?
Thanks
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
The URL-path matched by the RewriteRule pattern never starts with a slash. But you can use this to only match the last path-segment when there are more "folders". And the target URL also needs to end in .html (as per your examples).
So, this can be written in a single directive:
RewriteRule /([^/]+\.html)$ /$1 [R=301,L]
This handles any level of nested "folders". But does not match /foo.html (the target URL) in the document root (due to the slash prefix on the regex), so no redirect-loop.
(No need for any preceding conditions.)
Here the $1 backrefence includes the .html suffix.
Just match the last part of the url and pass it to the redirect:
RewriteRule /([^/]+)\.html$ /$1.html [R=301,L,NC]
It will match any number of directories like:
https://www.website.com/dir1/page1.html
https://www.website.com/dir1/dir2/page2.html
https://www.website.com/dir1/dir2/dir3/page3.html
https://www.website.com/dir1/dir2/dir3/dir3/page4.html

redirect rule removing slash randomly

For some reason, on some rules the / is removed. I have more rules than this but for the first one in this case it will work correctly but the second one which could be anywhere in the list of rules, redirects to https://www.example.comnot-working-url instead of https://www.example.com/not-working-url. If this happened for all it would perhaps make more sense but it only does this for some urls. Why would this be?
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^en/(.*) /$1 [L,NE,R=301]
Redirect 301 /en/working-url https://www.example.com/working-url
Redirect 301 /en/not-working-url https://www.example.com/not-working-url
You likely have a "catch-all" Redirect directive that is missing the trailing slash.
For example, if you have the following at the end of your rules:
Redirect 301 / https://www.example.com
Note the missing trailing trailing slash on the target URL.
Then a request for /not-working-url would be "erroneously" redirected to https://www.example.comnot-working-url by the above rule.
The Redirect directive is prefix matching and everything after the match is copied onto the end of the target URL. So, in the case of the above rule, not-working-url (after the initial / that matches) is copied onto the end of https://www.example.com. With the Redirect directive, the trailing slash should nearly always match on the source and target URLs, otherwise you'll likely get missing slashes or double slashes in the redirect response.
RewriteCond %{QUERY_STRING} .
RewriteRule ^en/(.*) /$1 [L,NE,R=301]
Redirect 301 /en/working-url https://www.example.com/working-url
Redirect 301 /en/not-working-url https://www.example.com/not-working-url
Without further explanation, these rules don't make too much sense. The RewriteRule directive removes the /en prefix (when a query string is present), so the Redirect directives that follow would not match?

How can I redirect users from a url to another using regex and htaccess?

I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.

Apache LocationMatch regex ignores duplicate slashes

I'm having a strange behaviour with Apache's LocationMatch directive when there are extra slashes at the beginning of the URL. According to the Apache docs if I'm reading it right this should work:
<LocationMatch ^/appcontext/(a|b)>
SetHandler weblogic-handler
WebLogicCluster apphost01:xxxx,apphost02:xxxx
WLProxySSL ON
</LocationMatch>
However if I type the following URL it is also being forwarded to the backend hosts:
https:// <hostname:port> ////////appcontext/a/
In the Apache docs it clearly states that it should apply the directive only for /appcontext/a/, unless I'm missing something with the regex or there is some issue with the mod_wl plug-in
http://httpd.apache.org/docs/2.2/mod/core.html
"For example, <LocationMatch ^/abc> would match the request URL /abc but not the request URL //abc"
Does anyone have any suggestion to achieve this?
Yes Apache does collapse extra slashes in rewrite rule. You can use THE_REQUEST variable to match and remove extra slashes. Place this in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule ^ %1/%2 [R=302,L,NE]