redirect if url doesn't match parameter - regex

I have a website incorrectly indexed on Google, like this:
www.mysite.fr/de-DE/mypage
I need to fix it, with a 301 redirect to www.mysite.de/de-DE/mypage
To put it simple, the first level domain (.it, .de, fr...) should correspond to the second url parameter (it-IT, de-DE, fr-FR...).
How can I do it using .htaccess only?

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI}::%{HTTP_HOST} ^/([a-z]{2})-.*?::(?:www\.)?mysite\.\1$ [NC]
RewriteRule . http://www.mysite.%1%{REQUEST_URI} [R=302,L,NE]

Related

.htaccess redirect URL with id param to human friendly URL

I'm trying to create a URL redirect but so far everything I have tried just hasn't shown any effect on the site. I know ModRewrite is enabled as there are other rewrites taking place. The whole purpose of this is to handle old URLs from the former version of the website.
What I want to achieve is a redirect of a URL with the following format:
/resources/view?id={id} and redirect it to /resources/{id}.
I've been trying to do so with variants of this:
RewriteRule ^resources/view?id=([0-9+])$ /resources/$1 [R=301,L]
and also this:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^/resources/view$ /resources/$1? [R=301,L]
Cheers.
You can use these 2 rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /resources/view\?id=(\d+) [NC]
RewriteRule ^ /resources/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^resources/(\d+)/?$ resources/view?id=$1 [L,QSA,NC]

.htaccess Redirections with custom parameters

I want to redirect the following link using .htaccess file
Old Url : localhost/info.php?butter_cake-cid111.html
To
New Url : localhost/butter_cake/cid/111.html
Thanks in advance.
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([^-]+)-([a-z]+)(\d+\.html)$ [NC]
RewriteRule ^info\.php$ /%1/%2/%3? [L,NC,R=302]
RewriteCond is matching and capturing all the values that are to be used later in target URL. ? in the end will strip off previous query string.

htaccess redirect part of filename

I have read several pages on redirecting files and urls with regex in htaccess, but nothing for what I am trying to achieve.
We have old file names such as /images/imagename-70-70.jpg - these have since been replaced by 100x100 versions, so the file name is now /images/imagename-100-100.jpg
We are getting a lot of errors in our log file for requests for the old filename. Is there any way to redirect based on splitting the file name up?
eg:
/images/$1-70-70.jpg should redirect to /images/$1-100-100.jpg
thanks for any help.
This should work:
RewriteEngine on
RewriteRule ^images/(\w+)-70-70\.jpg$ images/$1-100-100.jpg [L,R=301]
Keep this as your very first rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+images/(.+?)-70-70\.(\S+) [NC]
RewriteRule ^ /photos/%1-100-100.%2 [L,R=301]

Redirect to different domain and URL structure

I need to permanently redirect part of a website to a different domain and URL structure.
Everything that matches:
example.com/year/month/day/page-name/
and
www.example.com/year/month/day/page-name/
Needs to redirect to:
www.sample.com/sub/sub2/year/month/page-name/
For example:
example.com/2013/11/09/a-great-page/
should permanently point to:
www.sample.com/sub/sub2/2013/11/a-great-page/
But I don't want to redirect any other pages on the site:
example.com/a-random-page
Shouldn't redirect anywhere.
My htaccess chops and regex skills are a little rusty, so any help would be greatly appreciated!
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^([0-9]{4}/[0-9]{2}/[0-9]{2}/[^/]+/?)$ http%2://sample.com/sub/sub2/$1 [L,QSA,R=301]

How to redirect from an old php script to a new php script using mod_rewrite

It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]