Im trying to replace all %20 with + signs but just when the url starts with /some/dir/. For example:
http://mydomain.com/some/dir/replace%20here
should be redirected to
http://mydomain.com/some/dir/replace+here
but
http://mydomain.com/test/dir/replace%20here
should not replace the %20
So far I have:
RewriteCond %{REQUEST_URI} ^(/some/dir/)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1$2+$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1+$2 [L,R=301,DPI]
but that stills replaces the %20 in /test/dir
What am I missing?
Thanks!
Your rules are not correct and moreover DPI flag is not supported in all Apache versions. Change your Rewrite rules to this:
RewriteRule ^(some/dir)/([^\s%20]+)(?:\s|%20)+([^\s%20]+)(.*?)/?$ $1/$2+$3$4 [L,NC,R=301]
RewriteRule ^(some/dir)/([^\s%20]+)(?:\s|%20)+ $1/$2 [L,NC,R=301]
Related
I need to redirect any requests with query strings from a set of origin URLs back to a thank you page.
For example, I need to redirect:
http://example.com/test1/test2/[origin]/?id=1
back to
http://example.com/thank-you
The way I've got it set up in my .htaccess file is as such:
RewriteEngine On
RedirectMatch 302 ^/test1/test2/(.*)/.+ /thank-you
I've tested the regex I'm using in an online regex tester and it appears to work as expected, so I'm confused as to why the redirect isn't taking place. Here's the link to that.
Obviously, I had to add backslashes to escape the slashes in the URL in the regex tester, but based on my understanding of how .htaccess evaluates regex, these aren't necessary.
My question is: the redirect works perfectly from the page without the query string if I remove the .+ from the end of the regex string, meaning that the beginning part of the regex works fine. I don't understand why the query string isn't matching the regex I've created.
I have also tried:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L]
For your RedirectMatch, you may use:
RedirectMatch 302 ^/test1/test2/(.*)/(.*)+ /thank-you?
For your RewriteRule section, you may use:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L,QSD]
First , no need to RewriteEngine On with mod_alias which is RedirectMatch at your rules use it with mod_rewrite , the second rules .
Try this :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^test1/test2/[^\/]+/$ /thank-you? [R=302,L]
I use ^id=([0-9]+)$ to restrict query string for a one that start with id and end with numerical value.
I remove this line RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/ becasue you could match against URI in RewriteRule as well.
If this rules wrok , change [R=302,L] to [R=301,L] to be permanent redirection.
Note: clear browser cache then test
I have a rule in my htaccess file to remove any extra trailing slashes from a url, this works on sub-directories with any more than 1 trailing slash. However it doesn't work on the root; which i need it to do.
For example.
http://www.example.com/test//// Redirects to http://www.example.com/test/
http://www.example.com/// Needs to redirect to http://www.example.com
Any ideas on what i need to add?. Cheers.
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]
For removing multiple slashes anywhere in REQUEST_URI this rule works best:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]
It takes advantage of the fact that mod_rewrite engine itself converts all multiple forward slashes to a single slash in the RewriteRule pattern. We use RewriteCond %{THE_REQUEST} to make sure original REQUEST_URI contains multiple slashes.
Here [^?]*// matches 2 // before matching query string since [^?] matches anything except ?. This will allow // in query string.
Try with:
RewriteCond %{REQUEST_URI} ^(.*?)//+$
RewriteRule ^ %1/ [R=301,L]
You htaccess works great as you can test on below link
https://htaccess.madewithlove.be/
So you need to make sure you test either with a Chrome Incognito window or using like below
curl -v http://example.com////
I usually prefer curl as I know it will give a fresh response from the server always
You just need two rule to match two different pattern
RewriteCond %{REQUEST_URI} ^(?:/){2,}$
RewriteRule . / [R=301,L]
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]
I need to replace %20 white spaces in File names of requested URLs.
I'm using this Regex: ^(.*)\s(.*)$ which replace all of white spaces in the Url. I'm unable to make it working only for file names.
http://example.com/subfolder/One More Sub Folder/Onother Sub Folder/File%20test%20example.jpg
I'm under wordpress and I need a RewriteCondition and RewriteRule to make this:
http://example.com/wordpress/subfolder/One More Sub Folder/Onother Sub Folder/File-test-example.jpg
I also need same replace for htm requests, but I need to remove html extension from file names, too.
So now I have these Rules that are working for html:
RewriteCond %{THE_REQUEST} (\.htm|\.html)
RewriteRule ^(.*)(\.htm|\.html)$ http://www.example.com/wordpress/$1 [R=301,L]
and these for redirecting jpg:
RewriteCond %{REQUEST_URI} !^/wordpress/wp-content/uploads/
RewriteRule ^(.*\.jpg)$ /wordpress/wp-content/uploads/2016/02/$1 [R=301,L]
At the beginning of the htaccess file I have the lowercase transformation needed for wp slug to work:
<IfModule mod_speling.c>
CheckSpelling on
</IfModule>
If those are existent files, you can use the following rule to replace %20 with - :
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\s(.*)$ /$1-$2 [NC,L,R]
The rule is applied only for existent files.
Did you try RewriteRule ^(.*)(%20)(.*)$ /$1-$2 [R=301,NE,L]?
I have the a rewrite rule where I am trying to remove any dots and parentheses from the URL and I managed to get get to the point where I can do one or the other but just can't seem to combine both. How would I write the rule so that if any url has a dot or parentheses, they get removed? Thanks.
RewriteEngine On
RewriteCond %{REQUEST_URI} [\(\)\.]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ /$1$2$3 [L,R=301]
Try:
RewriteRule ^(.*)[().](.*)$ /$1$2 [E=DOT:Y,DPI]
RewriteCond %{ENV:DOT} Y
RewriteRule ^([^().]+)$ /$1 [L,R=301]
This uses an environment variable as a way to know when to redirect. Any number of parentheses and dots will get removed and when they're all done, the URL gets redirected.
Ok. I got this problem I trying to remove the last slash in a file url for example http://domain.com/styles/styles.css/. I got the code for adding slash to the end but cannot figure how to do the conditional.
If the URL has an extesion then remove end slash
else add slash..
Here what I got right now some blogs says its the solution but still isn't working for what I expect.
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
Also a problem, when I type http://domain.com/index it goes to http://domain.com/inde/.
Need your help guys.. Thanks a lot in advance.
add following code in your htaccess, for better understanding.
RewriteCond %{HTTP_HOST} !^\.yourdomain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
I have a link : domain.com/folder/
it will change to : domain.com//folder
also You can turn off mod_dir's redirect by including a DirectorySlash Off.
Why do you want to do an external redirect for such "furniture" files? Surely an internal redirect is what you want here?
Options -MultiViews
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)/$ $1 [L]
I advise that you turn off mutliviews if you don't use it as this can generate subrequests which confuse things.
Your RewriteCond conditions are logically inverted because you have the ! operator there. So the rewrite is applying only for those inputs which do not have extensions, and which do not have a trailing slash!
You can do this with a single rule with no conditions:
# Match any sequence of characters, ending in a dot followed
# by one or more characters that don't contain dots or slashes,
# followed by a final trailing slash.
#
# Rewrite this absolutely and treat as redirect.
RewriteRule ^(.*\.[^./]+)/$ /$1 [L, R=301]