.htaccess RewriteRule with slash in url - regex

What do I need to do to the following rewrite rule with slash in the url?
http://www.website.com/p/EQUITY+INVESTMENT+CORP%2FGA
to
http://www.website.com/test.php?name=EQUITY+INVESTMENT+CORP%2FGA
I tried the following way but not working.
RewriteRule ^p/(.*) /test.php?name=$1 [PT]

Your rule is fine but %2F isn't allowed in URIs by Apache. To allow %2F to be encoded to / you need to add:
AllowEncodedSlashes On
in your <VirtualHost...> section or in global context of your Apache config.

Related

htaccess redirect starting URL expression to blog page

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.

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.

How to write this URL rewrite rule to trucate unwanted content?

Iam using apache mod_rewrite to redirect some URL. I been able to some of the URLS but none of my rules are able to caprure this
http://192.23.70.11/mome/GIS%20Endo/%2Fhome%2FGIS%2520Endo%2F
and redirect it to
http://192.23.70.11/mome/GIS%20Endo/
You can use this rule as your first rule in site root .htaccess or Apache config:
# this goes in httpd.conf
AllowEncodedSlashes On
RewriteEngine On
RewriteRule ^(/?mome/[^/]+)/.+$ /$1 [L,NC,R=301,NE]
This will match any URI starting with /mome and will truncate anything after 2nd path component.

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]

simple .htaccess redirect : how to redirect with parameters?

My goal is simply to redirect:
/jsn.php?parameters to http://www.site2.com/jsn.php?parameters
I tried with
Redirect permanent /jsn.php(.)* http://www.site2.com/jsn.php$1
Query string parameters are automatically passed, you simply want to do this:
Redirect permanent /jsn.php http://www.site2.com/jsn.php
The (.)* doesn't work with the Redirect directive, you were probably thinking of RedirectMatch, but either way, you don't need it. And also (.)* should be (.*), otherwise the $1 backreference would only get the first character.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(jsn\.php)$ http://www.site2.com/$1 [L,NC,R=301]
You can use an explicit URL rewrite in your .htaccess file:
RewriteRule ^/jsn\.php\?(.*) http://www.site2.com/jsn.php?$1 [R]
Note: You need to escape . and ? because they are also regular expression characters.
If you have a problem using mod_rewrite, post the contents of your file.