htaccess regex to prevent rouge indexed url's - .htaccess

looking for a regular expression in my .htacess that will allow
http://example.com/index.php or http://example.com/anything-else
but redirect
http://example.com/index.php/anything-else
this is as close as I can come to the proper regex
RewriteEngine on
RewriteCond %{QUERY_STRING} index.php/
RewriteRule ^index\.php$ /send-them-to-404/? [L,R=301]

You can use AcceptPathInfo directive as I already mentioned in comments but if
urls were indexed by search engines then you need to use a 301 redirect to redirect them to a 404 page. 301 redirect updates previously indexed urls to the new ones.
RewriteEngine on
RewriteRule ^.+\.php/.+$ /404.php [L,R=301]

Related

How to redirect a URL wildcard using htaccess

My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.

.htaccess 301 rewrites with question marks

I'm using something similar to the following:
RewriteEngine on
Redirect 301 / http://newdomain.co.uk/link/
Redirect 301 /showcase.asp?showcaseid=1 http://newdomain.co.uk/track1
Redirect 301 /showcase.asp?showcaseid=2 http://newdomain.co.uk/track2
Redirect 301 /showcase.asp?showcaseid=3 http://newdomain.co.uk/track3
Redirect 301 /showcase http://newdomain.co.uk/link/tracks
With that in mind any URL other than the mentione would go to http://newdomain.co.uk/link
Which is fine however any of the other URL's that use a "?" go to say http://newdomain.co.uk/link/showcaseid=1 for /showcase.asp?showcaseid=1
Also with the
Redirect 301 /showcase http://newdomain.co.uk/link/tracks
can I just write as follows:
Redirect 301 /showcase tracks
You can't match against the query string (everything after the ?) in a Redirect directive. You have to use mod_rewrite's %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showcaseid=([0-9])
RewriteRule ^showcase\.asp$ http://newdomain.co.uk/track%1 [L,R=301]
RewriteRule ^showcase$ http://newdomain.co.uk/link/tracks [L,R=301]
RewriteRUle ^$ http://newdomain.co.uk/link/ [L,R=301]
Note that the order is important. You generally want the more general matching rules to be at the end.

How to fix a htaccess 301 redirect that contains a ? symbol

A new website has just gone live and there is a htaccess file which has 301 redirects in order to direct people from the old pages on the old domain to the new pages on the new domain.
However, because there is a ? and = symbol in the links it's not working.
I understand I'll need to take advantage of the query string, but I can't work out how to get these three examples working.
Redirect 301 /index.cfm?task=what_we_do http://domain.com/services/
Redirect 301 /pagecontent/_newsitem.cfm?newsid=63 http://domain.com/name-of-article/
Redirect 301 /pagecontent/_people.cfm?peopleid=3 http://domain.com/about-us/meet-the-team/john-smith/
Can anyone help?
You can't use a query string in the redirect. you have to use mod_rewrite.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^task=what_we_do$
RewriteRule ^index.cfm http://domain.com/services/? [R=301,L]
RewriteCond %{QUERY_STRING} ^_newsid=63$
RewriteRule ^pagecontent/_newsitem.cfm http://domain.com/name-of-article/? [R=301,L]
RewriteCond %{QUERY_STRING} ^_peopleid=3$
RewriteRule ^pagecontent/_people.cfm http://domain.com/about-us/meet-the-team/john-smith/? [R=301,L]

How to 301 redirect with same url structures?

I am trying to redirect site a to site b with same url structures. My htaccess is
Redirect 301 / http://www.siteb.com/
RewriteRule ^(.*)/(.*)/(.*)$ index.php?video=$1&id=$2&words=$3
RewriteRule ^videos/(.*)$ index.php?videos=$1
However, on the redirected site, it adds additional variables. For example;
siteA.com/y/T5INF08ZEdA/Miranda-Kerr-In-Hot-Water-with-Orlando-Bloom
siteB.com/y/T5INF08ZEdA/Miranda-Kerr-In-Hot-Water-with-Orlando-Bloom?video=y&id=T5INF08ZEdA&words=Miranda-Kerr-In-Hot-Water-with-Orlando-Bloom
What is the correct way to 301 redirect with same url structures ?
It is because of the 2 main problems in your code:
Mixing of mod_alias and mod_rewrite rules.
Not using L flag in mod_rewrite rules and that is causing multiple rules firing on same URL.
I would suggest you to stick with mod_rewrite only and keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)sitea\.com$ [NC]
RewriteRule ^ http://www.siteb.com%{REQUEST_URI} [L,R=301]
RewriteRule ^videos/(.*)$ index.php?videos=$1 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/(.*?)/?$ index.php?video=$1&id=$2&words=$3 [L,QSA]

Redirect in htaccess is adding directory to new URL; not redirecting ExpressionEngine URLs correctly

I'm trying to setup a redirect rule for a new ExpressionEngine site.
I'm using the following code placed in my site's .htaccess file (the latter section is to remove index.php from ExpressionEngine's URLs - this works):
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch 301 ^/2010/example/*$ http://sub.domain.com/new-page/$1
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
A redirect of sort happens, but the URL I get is this:
http://sub.domain.com/new-page2010/example/
I've tried different combinations and it's driving me up the wall!! Any tips on where I'm going wrong?
The syntax for Apache's RedirectMatch Directive expects the use of parenthesis in the RegEx:
The supplied regular expression is matched against the URL-path, and
if it matches, the server will substitute any parenthesized matches
into the given string.
Using a RedirectMatch is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching.
Therefore correcting your example, a valid RedirectMatch Directive would be:
RedirectMatch 301 ^/2010/example/(.*)$ http://sub.domain.com/new-page/$1
You can tap in the full power of Apache's rewrite engine by using a RewriteRule instead:
RewriteRule ^2011/example/(.*)$ http://sub.domain.com/new-page/$1 [R=301,L]
Depending on what you're wanting to redirect — a single page vs. entire sub-directory — you may need to modify the original URL path in each example.

Resources