How to redirect ourdomain.ch/index.php?xyz to ourdomain.ch? - apache

Due to a past hacking of the site we have hundreds of indexed files in Google (site:ourdomain.ch) like this:
ourdomain.ch/index.php?02374234999
ourdomain.ch/index.php?wer99234234
1) These URL's are not redirected and show the content of the homepage, just with another URL (see above). I guess this is a problem SEO wise, i.e. duplicate content. Right?
2) I am not able to redirect these files to the root. No way what i do. I added that line to the existing .htaccess:
RewriteRule ^index.php\?123 /index.php [R=301,L]
just to test it with ourdomain.ch/index.php?123
it redirects to: ourdomain.ch/?123. Not what i want...
Updated .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php\?123 /index.php [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any hint how to accomplish that?
Thanks a lot for your help.
Cheers,
Cesare

You need to use RewriteCond
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^index\.php$ /index.php? [L,R=301]
This will redirect /index.php?foobar to /index.php removing the querystring from your index.php .

Related

Removing string and date from url with htaccess and rewrite rules

I would like to replace some parts of my url, year-month and also .html url extension.
Basically i want this ( old url )
https://olddomain.com/weblog/archives/2016/06/post-name-example.html
to redirected to
https://newdomain.com/post-name-example/
this url. I have searched stackoverflow and many other sites, tried mixing different answers from different people together. Tried writing my own rules but i couldn't pull it off. Any help would be much appreaciated. Thank you.
I tried this to remove .html extension
#1)redirect /file.html to /file
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NE,L,R]
This to remove date format from url
# REMOVE DATE FROM WP POST PERMALINKS
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/$4
http://example.com/wordpress/2012/04/12/post-name/
..to elegance:
http://example.com/post-name/
My htaccess file is like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^weblog/archives/[0-9]{4}/[0-9]{2}/(.*)\.html$ https://newdomain.com/$1/ [R=301.L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This worked properly if anybody needs it in the future:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^weblog/archives/[0-9]{4}/[0-9]{2}/([^.]+)\.html$ http://www.newdomain.com/$1/ [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Rewrite rule for opening a different page

I'm stuck with some .htaccess rules.
I've got the page /module/slug. I want, when I type slug to see content of /module/slug/ but not redirect to this page. Is it possible? I've tried this rule, but had no luck:
RewriteRule ^slug/$ /module/slug/ [L]
My .htaccess content:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^calculator/$ /sample-page/ [R=301,L]
</IfModule>
# END WordPress
That happens because you don't tell Apache to redirect (R=301).
Add the redirect flag R, and it should work. Also make sure that:
You have set the RewriteBase correctly.
There are not any other rules earlier in the file that match the same URL.
.
RewriteEngine On
RewriteBase /
RewriteRule ^slug/$ /module/slug/ [R=301,L]

Apache .htaccess RewriteRule not including subdirectory when redirecting non-www to www url

I've setup a rewrite rule in order to direct domain.com traffic to www.domain.com using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
From my limited understanding, the $1 in the RewriteRule should match anything after domain.com/ and then be placed at the end of my rewritten url www.domain.com/.
For example domain.com/abc should become www.domain.com/abc.
However this is not working. If I browse to domain.com/abc then this path isn't rewritten at all and I just get domain.com/abc without the www.
I've done lots of reading just to figure out that the $1 should be taking care of this, from my understanding.
Can anyone explain why it isn't working as I suspect it should be? Thanks.
It turns out that because I was using Wordpress AND W3 Cache plugin that I was simply putting the code in the wrong place in my .htaccess file.
The code should be placed within the # BEGIN Wordpress codeblock and before the existing rules that Wordpress already has in place.
The existing code should look something like this:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
I added my original code to this as follows:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
This fixes the issue I was describing above. Thanks to the commenters for sparking my thought process.

rewrite htaccess from xyz.com/?type=test&s=test2 to xyz.com/test/test2

what i need is for a search thing, so the test2 is changeable and need the link to change too.
how can i do this in .htaccess?
this is what i thought to do.
RewriteRule ^xyz.com/test/(.*)$ ^xyz.com/?type=test&s=$1 [NC,L]
any idea?
Thanks!
my Wordpress .htacess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^([^/]+)/(.*)$ index.php?type=test&s=$1 [NC,L]
</IfModule>
# END WordPress
You will be looking at something like this:
RewriteRule ^([^/]+)/(.*)$ ?type=$1&s=$2 [NC,L]
Not sure it will work without the filename, though. If that's the case point the rewrite to your file that handles requests, e.g.:
RewriteRule ^([^/]+)/(.*)$ index.php?type=$1&s=$2 [NC,L]
You don't include the domain name in the RewriteRule match pattern.

mod_rewrite redirect directory to parent directory

Recently I moved my site to Amazon EC2 instance. I want to redirect requests for
http://www.domain.com/data/
to
http://www.domain.com
so any previous paths in the old website like
http://www.domain.com/data/kjk/sds/sds/index.html should become http://www.domain.com/kjk/sds/sds/index.html
I have tried a couple of mod_rewrite rules using htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^data/. / [R=301,L]
</IfModule>
Any ideas?
Maybe something like this?
(not tested!)
RewriteRule ^data/(.*)$ /$1 [R=301,L]