We have a bunch of URLs that were indexed by Google with special apostrophes (url encoded as '%E2%80%99'). We corrected the urls on the server, but Google is still pointing there and we didn't want to interrupt any SEO mojo here. Any thoughts why this won't work?
Current rewrite rule in .htaccess file:
# remove apostrophes from a string
RewriteRule ^(.*)’(.*)$ /$1$2 [L,R=301]
RewriteRule ^(.*)%E2%80%99(.*)$ /$1$2 [L,R=301]
Example replace this URL:
http://example.com/santa%E2%80%99s-comin-to-town/
with this URL:
http://example.com/santas-comin-to-town/
Try using this:
RewriteRule ^(.*)’(.*)$ /$1$2 [B,L,R=301]
RewriteRule ^(.*)([^\w].+\d)(.*)$ /$1$3 [B,L,R=301]
using the % character can have adverse effects on rewrite rules:
(%..%..%..) or (\%..\%..\%..)
should also work, although make sure you provide the [B] flag on the end of the rule.
more info
Use this rule for using hex code in rewrite rules:
RewriteRule ^(.*)\xE2\x80\x99(.*)$ /$1$2 [L,R=301]
Related
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 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.
Sorry for this probably very noob-style question, but I just can't get it to work.
Here's my current .htacces file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^_res/.*$ - [L] # all URLs beginning with "_res" are not changed.
# put new fancy rule doing what I want here...
RewriteRule . index.php [L] # all other URLs are redirected to index.php
</IfModule>
This works fine. It's redirecting all urls except the ones starting with "_res/" to index.php.
What I need now is that all URLs matching "[anypath]/video/[anyfilename].mp4" will be rewritten to "content/[anypath]/video/[anyfilename].mp4" (which is the actual path of this file on the server - I can't use x-sendfile on this shared web space, thus I need to rewrite large file urls to their actual server locations to avoid php's fileread-function).
From my understanding of these RewriteRules, I think I have to place this rule just before the last one.
Unfortunately my regexp-expertise is practically non-existent.
What I thought should work is this:
RewriteRule ^(.*)/video/(^/*)\.mp4$ content/$1/video/$2.mp4 [L]
The regexp should mean "starts with any amount of any characters, followed by "/video/", followed by any amount of any characters which are not '/' and ends with ".mp4".
When I insert this into my .htaccess right before the last rule, urls ending with "/video/myvid.mp4" are still rewritten to index.php.
You see me clueless. Any suggestions?
Thanks alot in advance!
I think you want:
RewriteCond %{REQUEST_URI} !^/content/
RewriteRule ^(.*)/video/([^/]+)\.mp4$ content/$1/video/$2.mp4 [L]
And you want to add this rule above the one that routes to index.php
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]
I'm having a problem in .htaccess, specifically on redirection.
I wanted to rewrite this url:
http://www.domain.com/?mod=country
to
http://www.domain.com/country
I already tried this .htaccess code:
RewriteEngine on
RewriteRule ^\?mod=(.*)$ $1
I tried working on it without the question mark character and it works fine.
I have escaped the question mark because it's part of the special characters, but still, I couldn't get it work.
Can you tell what I'm missing?
Directly translating your requirements to rewrite rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mod=(.*)$
RewriteRule ^$ %1
But you probably want something more flexible like:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)mod=([^&]+)
RewriteRule /?(.*) %1/$1 [QSA]
These rules apply to the root of your domain.
If you want to do an external redirect add the R flag to the rewrite rule.