how to remove trailing %20target= from url using htaccess - apache

Recently i have been using stackoverflow to fix many of my server errors. Recently i have encountered the following error "NOT FOUND 404 error" for the following error.
I have seen from web master central that there are several hundred urls have trailing string "%20target=" I want to remove so that the url yield results.
here is the sample. i wanted to change the following url from
www.example.com/one/two/three/four/five/For-Sale-26264.html%20target=
to
www.example.com/one/two/three/four/five/For-Sale-26264.html
How can i achieve this using htaccess

The %20 is actually a URL encoded space, so you can use a lazy select (.+?) to match as few characters as possible before the space with \s and then target. The grouped match is available as a back reference with $1 for the rewrite.
RewriteEngine On
RewriteBase /
RewriteRule (.+?)\starget= $1 [L]

Related

Redirecting Many Dynamic URLs (301)

I have a website that is generating dynamic URLs through categories and it outputs the same information on two separate URLs (In this example it's "buildings" and "houses")
I would like to redirect all URLs that have /buildings/ in the URL to the same one with /houses/ instead.
For example:
/buildings/united-states/arizona/tucson/
to
/houses/united-states/arizona/tucson/
There are many URLs like this and I would like to use a code that does this for all.
I have tried
RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L], but it didn't seem to work (it still pointed to the /buildings/ URL.
Appreciate all your comments and guidance, thank you!
RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L]
For some reason have a \d (shorthand character class) that matches digits 0-9 only, so this won't match the example URL. Also, [^/]+ would seem unnecessary if it can be followed by anything anyway.
Try the following instead:
RewriteRule ^buildings/(.*) /houses/$1 [R=302,L]
This matches /buildings/<anything>. The $1 backreference holds the <anything> part.
Test first with 302 (temporary) redirect to avoid potential caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended. You should clear you browser cache before testing.
This needs to go near the top of your .htaccess file, before any existing rewrites.

htaccess rewrite rule with strings that contain slashes

I have a mod rewrite rule like the following.
RewriteRule ^bands/([^/]*)$ /folder/index.php?palabra=$1 [L]
It works except when someone access a link that uses a word with a / symbol on the band name, for example the band ac/dc would not work and shows a 404 error.
Im guessing some kind of problem with the regex.
How can I avoid this problem?

.htaccess -> Characters not escaping in rewrite string "(" ")"

I'm currently trying to add some redirects from a very old site that had PDFs and some of these PDFs have used brackets to contain the years.
For example:
/document%2070C11-name%20(2012).pdf
So the full URL I am trying to redirect is:
https://website.co.uk/document/1/document%2070C11-name%20(2012).pdf
I have previously setup many different redirects on this site but ones with the special characters seem to be causing an issue.
Here is the examples of what I have tried, and failed with so far:
RewriteEngine on
RewriteBase /
RewriteRule ^document/1/document%2070C11-name%20\(2008\).pdf$ document/newurl/ [R=301,L]
RewriteRule ^document/1/document%2070C11-name%20(.*)2008(.*).pdf$ document/newurl/ [R=301,L]
So I tried escaping the character and just trying a wildcard. Neither seemed to work on my Apache server. The code I used did seem to work when I tested it on:
http://htaccess.madewithlove.be/
But I am pretty stuck now and would love any help I can get.
Thanks,
Kane
To match %MN character in URL, you need to use \xMN in the RewriteRule pattern.
Hence this rule should work for you:
RewriteRule ^document/1/document\x2070C11-name\x20\(2008\)\.pdf$ /document/newurl/ [R=301,NC,NE,L]
This will redirect https://website.co.uk/document/1/document%2070C11-name%20(2008).pdf to https://website.co.uk/document/newurl/

How to include a single $ (dollar-sign) within the rewrite rule

I'm trying to create a rewrite rule for my .htaccess file. I want to include a single dollar sign within it. It is supposed to be something like this:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^\$$ "http\:\/\/domain\.com\/something" [R=301,L]
The problem is that it doesn't work with a single dollar sign, so if I go to domain.com/$ I get a 404. It works only with another letter, for example ^\$a$ - in such case domain.com/$a would redirect to domain.com/something.
The workaround is to create a new folder, rename it to $ and put the .htaccess file there, but I would rather avoid creating multiple folders with no content for such purposes. I couldn't find any reference on the Internet (the official Apache documentation for mod_rewrite was not very helpful). I tried using multiple slashes in different combinations but everything failed. Am I missing something or is it just impossible to make it work this way?
For me it works exactly as expected in htaccess with RewriteRule ^\$$ and requesting "$". Have you looked at the rewritelog / loglevel rewrite:trace8 yet?

How to remove % from end of url in apache rewrite

I've noticed an increase of requested URLs with a % at the end.
eg. http://sample.com/countries/usa%
We have an Apache rewrite rule the converts a properly formed request to the desired page on the server
RewriteRule ^countries/([a-zA-Z]+)$ /index.php?c=$2
However, when the user (or a bot?) adds the % symbol to the end it forces a 400 error.
Google's Webmaster Tools has found an increase of these type of errors and I haven't a clue how to remove it. I can't do it in PHP because the error is happening at the Apache level.
Any help will be most appreciated.
I would add something like this in the beginning of your .htaccess file:
RewriteRule ^(.*)\%$ $1 [R=301,L]
This causes a permanent redirect (because of R=301) to the page without the '%'