Remove dot and parentheses from the URL in rewrite rule - regex

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.

Related

Remove multiple trailing slashes in root using htaccess

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]

Apache RewriteRule matching ^abc/?(.*)$ and thus missing ^abcd/?(.*)$

I have a programmatically generated set of rules for a site like this:
RewriteEngine on
RewriteRule ^abc/?(.*)$ /$1?organisation=abc [QSA,L]
RewriteRule ^fghij/?(.*)$ /$1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn/?(.*)$ /$1?organisation=fghijklmn [QSA,L]
So that our client can set up multiple minisites on their domain for various clients of their own.
Because the url could end in / or /blah.php I've created the rules as above but this means the RewriteEngine would stop after ^fghij/?(.)$ and never find ^fghijklmn/?(.)$
How could I rewrite my rules so every organisation is properly matched?
Many thanks in advance
Your regex seems to be a problem since ^fghij/?(.*)$ would also match fghijklmn.
Try this code:
RewriteEngine on
RewriteRule ^abc(/.*)?$ $1?organisation=abc [QSA,L]
RewriteRule ^fghij(/.*)?$ $1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn(/.*)?$ $1?organisation=fghijklmn [QSA,L]
I think that the /? in ^abc/?(.*)$ is saying optionally match / then match anything else after it so you likely want something like
RewriteRule ^abc//?(.*)$ /$1?organisation=abc [QSA,L]
That will enforce a / at the end since you say the URL will always end in / or /blah.php
Also, I am unsure why you don't just try to match the organisation with a non-greedy matcher on the word before the first slash and then append it to the query string instead of creating it manually.

Rewrite rule in htaccess for nice links and I need advice

I'm trying to make nice links in my apps.
I decided to rewrite links that look like these:
1. http://example.cz/get1/get2/get3
2. http://example.cz
Into these (I have php appliactions only):
1. http://example.cz/index.php?path=get1+get2+get3
2. http://example.cz/index.php?path=
I'm removing www before links.
I keep failing to rewrite it into .htaccess.
I'm also looking for advice if the primary idea of rewriting get params into path=get1+get2+get3 is good? Right now I can see that link like this http://www.example.cz/you+me/ could possibly fail somewhere. Do you have any better solution?
So question is: How to rewrite it into .htaccess and how to solve possible problems with link that contains '+'
EDIT:
I improved my skills a little and I did this:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.php?path=/$1 [R=301,L] # 301 is here so I can see how does it work
# everything above works well (as I want)
# link now look like this one:
# http://example.net/index.php?path=/get1/get2/get3
# Now I was looking for universal rule that will rewrite my get params...
# First I did this:
RewriteCond %{REQUEST_URI} /([^/]+)
RewriteCond %1 !index.php(.*)
RewriteRule /([^/]+) $1+ [R=301,L]
# If any part of request uri is string that matches pattern /([^/]+)
# And if ([^/]+) doesn't match index.php(.*)
# then rewrite every /([^/]+) into $1+
# Now I see that it is incorrect, but I wasn't able to fix it
# So then I did different rule
RewriteRule ^([^=]+=[^\/]*)\/([^\/]+)(.*)$ $1$2+$3 [R=301,L]
# start of string
# first var is: one or more chars except =, =, zero or more chars except /
# /
# second var is: one or more chars except /
# third var is: zero or more chars
# end of string
I think second idea was much more better, but it doesn't work too. Please help me to fix it.
You can do this with the Apache module mod_rewrite. Chances are you probably already have it installed. Try this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?path=$1+$2+$3 [L]
This regex assumes the URL will always contain three groups of text between slashes. You can tweak it as needed.
Also note that Apache never sees the URL hash, so you won't be able to match it in a rewrite rule. Luckily, it looks like you don't want to do anything with it anyway. Just use the rule above, and the hash will remain at the end of the URL in the browser.
I did solution. Problem was that after adding index.php?path= I wasn't able to work with query string...
The final universal solution that turn links from http://www.example.net/get1/get2/get3 to
http://example.net/index.php?path=get1+get2+get3:
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.phppath=/$1 [R=301,L]
RewriteRule ^([^=]+=[^/]*)/([^/]+)(.*)$ $1$2+/$3 [R=301,L]
RewriteRule ^(.*)\+/+$ $1 [R=301,L]
RewriteRule ^(.*)path=(.*)$ $1?path=$2 [R=301,L]

mod_rewrite trailing slash for directory and remove for file url

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]

How to Redirect Subdomains to Other Domain

What I'm trying to accomplish with htaccess mod-rewrite:
Redirect all sub-domains to new domain name w rewrite rule.
e.g.
test1.olddomain.com ===> test1.newdomain.com
test2.olddomain.com ===> test2.newdomain.com
test3.olddomain.com ===> test3.newdomain.com
This is what I have so far which of course is wrong:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [R=301,L]
RewriteRule [a-zA-Z]+\.olddomain.com$ http://$1.newdomain.com/ [R=301,L]
Since I'm not a Regular Expression junkie just yet, I need your help... Thanks for any help you can give here. I know also we can compile these first two conditions into one.
Note: The reason I don't redirect all domain using DNS is that a lot of directories need special rewrite rules in order to maintain positions on SEO.
In .htaccess files, the "URL" that RewriteRules match has been stripped of the domain name and any directories that led to the current directory. (Using mod_rewrite in .htaccess files is a huge pain; if you have access to the server conf do it there instead!!)
So, assuming that your .htaccess is in your DocumentRoot, try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]
The %1 is supposed to match the first group in the RewriteCond and the $1 is supposed to match the URL part.
RewriteRule ^(.+)\.olddomain\.com$ http://$1.newdomain.com/ [R=301,L]
You need to specify the ^ at the beginning to ask the regex engine to match a line beginning there. Next, you match anything before ".olddomain.com" and assign that to the first matched pattern (which will later be accessible in $1). You need to surround with parentheses (.+) in order for the match to be assigned to $1.