Remove Last Character of URL with .htaccess - .htaccess

Somehow google managed to pickup some URLs on my site with a ) at the end which is causing a bunch of 404 links. The links look something like this:
"http://mysite.com/page.php?id=42523"
but what google has in its search results is:
"http://mysite.com/page.php?id=42523)"
Is there a way with .htaccess that I can determine if a url ends with ) and redirect it to the proper URL?

Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\)$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]

Related

Htaccess rewrites causing infinite loop

I am trying to write a redirect for a PDF file in my wordpress install that has shown up in my google webmaster tools with a load of query strings after it, what I want is to have the redirect match any number or query stings after the pdf filename and just redirect directly to the file.
RewriteRule ^(.*) http://www.example.com/wp-content/uploads/2014/08/test\.pdf$ [R=301,L]
and this also (as well as numerous different iterations)
RedirectMatch 301 ^/wp-content/uploads/2014/08/test\.pdf(.*)$ /wp-content/uploads/2014/08/test.pdf?
However they either result in a continuous redirect loop, 404 error, or still leave part of the query string
the url has any number of random query strings so something that would catch anything after the filename and just redirect to the bare file is percisely what I am looking for. here is an example of the url:
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr
or
http://www.example.com/wp-content/uploads/2014/08/test.pdf?test=324234234.ffdsfewrgfdgdfg.234234.234324.2333333&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr&fsdf=3432423.1.34234324&wer=werewrewr
and what I want to get is just
http://www.example.com/wp-content/uploads/2014/08/test.pdf
Thanks
Try something like this with mod_rewrite.
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} (.+)\.pdf$
RewriteRule ^(.*) /$1? [R=301,L]

.htaccess forward from one domain to a specific .html page

I know how to use .htaccess to forward everything in one domain to a new domain name. But in this case, I want everything from one domain to go to a specific .html page on a different domain. That's where I'm lost. I'm trying the following but it just redirects to a folder and the page in question is in that folder but obviously, I don't want people seeing the contents of that folder. Make any sense? So example.com needs to go to yyy.com/some-page.html
This is what I'm currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule ^(.*)$ http://www.1.yyy.com/$1 [R=301,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule .* http://www.1.yyy.com/some-page.html [R,L]
You can also put a blank index.html page to the directory in question to mask its contents or
you can put index.php file with this code <? header ("location: http://www.1.yyy.com/some-page.html"); ?> that will redirect a user to the desired page.
$1 is a place holder for the 1st pattern match. So if you are rewriting domaina.com/someurl/, it is attempting to load domainb.com/someurl/. Swap the $1 with the actual page --- e.g. somepage.html and it should work. But unless both of these domains are pointing to the same files/directories, the rule seems a bit overcomplicated.
So how about just a simple redirect?
Try this in your .htaccess file.
redirect 301 / http://somesite.com/somepage.html
OR you can try this.
RewriteRule ^(.*)$ http://somesite.com/somepage.html [R=301,L]
It does work and you can test my RewriteRule below.
http://htaccess.madewithlove.be/
There must be something else going on.

301 Dynamic Pages Redirect

Hi I tried various methods with modewriter in htaccess shown in other threads. I want to redirect my url from www.example.com/categories.php?category=1 to www.example.com/categories/science.
I would really appreciate an example too where I'm wrong. Thank You.
Assuming that it is just this one URL you want to redirect it would be like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^category=1$ [NC]
RewriteRule ^categories.php$ www.example.com/categories/science/? [L,R=301]
This is in case you are using a .htaccess file, otherwise add a prepending slash:
RewriteRule ^/categories.php$ www.example.com/categories/science/? [L,R=301]
The querystring wil be discarded. Only put [R] if you want to show it to the user in the addressbar of the browser. Consider which status you want to serve, Google likes 301 if it's permanent.
Next you have to catch the incoming request with an index.php in the directory www.example.com/categories/science
good luck!

.htaccess redirect non existing pages based on index.php page with wildcard and something after it to index.php

Please help, some website is arbitrarily adding dynamic query strings to my home page.
For example my home page is www.mysite.com/index.php and they link to many links like this:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
And those links are opening on my site, content of page are the same for every page, just like my original www.mysite.com/index.php
There are few hundreds of that links pointing to my site. So how I can redirect this:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
...
to
www.mysite.com/index.php or just to www.mysite.com/
This what I tried so far in my .htaccess file
RedirectMatch 302 ^index.php?a= http://www.www.example-ste.com/
RewriteRule ^/index.php?a=(.*) http://www.example-ste.com/
But still pages are opening on site.
Another similar question.
How to redirect pages ending with "?pagewanted=all" to the same page but with out that "?pagewanted=all"
For example I need to redirect page:
www.mysite.com/something-something/something.html?pagewanted=all
to
www.mysite.com/something-something/something.html
Hello.
I just noticed something. I needed URL redirection rule which will redirect pages like:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
to home page of site, root. And you gave me this code:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.mysite.com/? [R=301,L]
And I must say it works fine, it does that, but I just noticed that it somehow blocks or redirect all links containing ?a= for example on some temporary pages I have links like:
i.php?a=something-something-something
So, can you adopt code just for pages based on index.php like:
www.mysite.com/index.php?a=something-something-something
and not for links with:
i.php?a=something-something-something
If I am right it works on all links with "a=" but I need just for "index.php?a="
Try adding the folloginw to the top of your .htaccess file
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.mysite.com/? [R=301,L]
#if the query string has a pagewanted parameter
RewriteCond %{QUERY_STRING} (^|&)pagewanted=all [NC]
#Redirect and remove query string parameters for html pages
RewriteRule (.+\.html) http://www.mysite.com/$1? [R=301,L]
Edit.
Added rule to remove pagewanted=all

Redirect hundreds of urls with htaccess

hello my url structure right now for the majority of my links is:
www.url.com/category1/sample-keyword.html
I am looking to redirect them to the new url that has dropped the word sample from the url structure ie to this:
www.url.com/category1/keyword.html
what should i put in htaccess that auto redirects all the urls in the www.url.com/category1/ section to redirect to the new url structure?
This should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)sample-(.*)$ $1$2 [L,R=301]
It will match all URLS with the substring /sample- and strip it from the URL. Depending on your site organization, you may need to adjust the pattern, but that should be a good jumping-off point.
RewriteEngine On
# Redirect sample-*.html to *.html
RewriteRule ^\/?category([0-9]+)\/sample\-([^\/]+)\.html$ http://www.url.com/category$1/$2.html [R=301]
# Serve *.html
RewriteRule ^\/?category([0-9]+)\/([^\/]+)\.html$ page.php?category_id=$1&keyword=$2 [L]

Resources