I'm moving an existing projet from root to language folder /fr, in order to create a different english project located in /en.
The project is simple, it consists on two pages (index.php and liste.php).
The first page was receiving a parameter
/index.php?hash=[page-hash]
and was rewriten in the .htaccess as
/[page-hash].html
So here is the content of the old .htaccess:
RewriteEngine On
######### Page rewriting ########
RewriteRule ^liste\.html$ liste.php [QSA,L]
RewriteRule ^(.*)\.html$ index.php?hash=$1 [QSA,L]
I want the old rewritten pages (www.domain.com/[page-hash].html), which are indexed by Google, to be 301 redirected to www.domain.com/fr/[page-hash].html so that Google still find them at new new location and is informed it has been moved.
It's not a rewriting, it's a redirect, and I don't know how to do that. I guess I have to use RewriteCond and [R=301] but I don't know how.
Of course the new pages must also be rewritten (/fr/index.php?hash=[page-hash] into /fr/[page-hash].html), and the root generally redirected to /fr. So I already made this new .htaccess:
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr
Redirect 301 /liste.html /fr/liste.html
Redirect 301 /liste.php /fr/liste.html
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [QSA,L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [QSA,L]
RewriteRule ^en/list\.html$ /en/list.php [QSA,L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [QSA,L]
You're pretty close. You can condense those and you only need QSA (and B) when your substitution contains a query.
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr/ [L,R=301]
RewriteRule ^([^/]+)\.(?:html|php)$ /fr/$1.html [L,R=301]
######### Page rewriting ########
RewriteRule ^(fr/liste|en/list)\.html$ $1.php [L]
RewriteRule ^(fr|en)/([^/]+)\.html$ $1/index.php?hash=$2 [L,B,QSA]
Here is the complete answer. The order is important.
RewriteEngine On
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [L,B,QSA]
RewriteRule ^en/list\.html$ /en/list.php [L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [L,B,QSA]
################ Redirect from root to /fr ################
RewriteRule ^$ /fr [L,R=301]
RewriteRule ^liste\.html$ /fr/liste.html [L,R=301]
RewriteRule ^(.*)\.html$ /fr/$1.html [L,R=301]
This can still be condensed (see Walf's answer).
Related
I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]
I need to change the following url
http://somedomain.com/news/a_sample_news_article.html
to
http://somedomain.com/post/a-sample-news-article
I have this in my htaccess which works, but I am sure it can be improved upon - does anyone have a better solution?
RewriteEngine on
# replace underscores
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302]
# redirect the directory from news to post
RewriteRule ^news/(.*)$ /post/$1 [R,L]
# remove .html from end of url
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
Any help much appreciated!
To redirect
/news/foo_bar
to
/post/foo-bar
you can use the following rule :
RewriteEngine on
# redirect "/news/foo_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]
Basically I have 2 different rules/tasks which should be combined in one working set of rule.
Task 1
All non-ssl pages have to be redirected to ssl. My solution is already working on its own.
Dummy example:
non-ssl://sample.some-website.ch/de/test.html -> ssl://sample.some-website.ch/de/test.html
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^sample.some-website.ch$
RewriteRule ^(.*)$ "https\:\/\/sample\.some\-website\.ch\/$1" [R=301,L]
Task 2
All old .phtml Links have to be redirected to new SSL pages (individually). My solution is working on its own (2 dummy examples)
non-ssl://sample.some-website.ch/xyz.phtml -> ssl://sample.some-website.ch/de/newlink.html
non-ssl://sample.some-website.ch/123.phtml -> ssl://sample.some-website.ch/en/something.html
redirect 301 /xyz.phtml https://sample.some-website/de/newlink.html
redirect 301 /123.phtml https://sample.some-website.ch/en/something.html
Problem
I need a .htaccess solution which combines Task 1 and Task 2.
Certain old .phtml (non ssl) links have to redirected to new specific SSL-Pages AND all 'other normal' non-ssl links have to be automatically redirected to its corresponsing SSL-pages.
In the end only ssl-pages are displayed.
Any ideas?
Thanks. Hans.
You can have your .htaccess like this:
RewriteEngine On
RewriteBase /
# redirect old links
RewriteRule ^xyz\.phtml$ https://sample.some-website/de/newlink.html [L,R=301]
RewriteRule ^123\.phtml$ https://sample.some-website.ch/en/something.html [L,R=301]
# add https to your site
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# route .html file to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
# earlier rules to rewrite to index.php
RewriteRule ^[a-z]{2}(-[A-Z]{2})?/$ index.php [L]
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)$ $1/ [R=301,L]
I'm looking for a solution to redirect old urls (e. g. aboutme.php) to the new ones (same /about-me).
The problem is: if I'll go to example.com/aboutme.php, the user is not redirected to pretty url (/about-me). Adding R=301 doesn't help - it makes /about-me redirect to aboutme.php.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]
</IfModule>
You have to make it in the other direction.
If you want to rewrite aboutme.php to about-me
you should use
RewriteRule ^aboutme.php /about-me [R=301,L]
EDIT:
The place /about-me has to be present. If not, you can redirect this virtual place to a file (say rewrite.php) which gets the actual file from database or some configuration. Place the following rules at the bottom of your htaccess rules.
#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]
#as example don't rewrite the image folder
RewriteRule ^images - [L]
#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
after that you can access your requested virtual file in rewrite.php with $_GET['file'].
EDIT:
your rewrite.php could look like this: (this is the simplest way todo. but you have to do some error checking etc.)
<?php
$file = $_GET['file'];
$config = array(
'about-me' => 'aboutme.php'
);
include($config[$file]);
?>
I am trying to implement 301's for sub-pages on my site which is hosted on an Apache server (Fasthosts), using Rewrite scripts in the .htaccess file. I have tried to follow many pieces of documentation (infact, I've never had any problems implementing 301's using .htaccess before!) but on this particular website, nothing seems to work.
There is currently a 301 re-direct in there from non-www to www which is working fine. There are also some other snippets using regex which I imagine are for the CMS.
Below is the current state of the .htaccess file. An example of a 301 I'm trying to re-direct is in there (Lines 5 & 6)
Old page: http://www.junkwize.com/home-Garden%20Clearance
to New page: http://www.junkwize.com/services/garden-clearance-london
.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^junkwize.com
RewriteRule (.*) http://www.junkwize.com/$1 [R=301]
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
#Options +FollowSymlinks
RewriteRule ^.htaccess$ — [F]
RewriteRule ^([/admin]+)$ admin/login.php [L]
RewriteRule ^([/admin]+)([/blocks]+)$ admin/login.php [L]
RewriteRule ^([/blocks]+)$ index.php [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)-([^/\.]+)$ index.php?main=$1&id=$2&menu=$3 [L]
RewriteRule ^([/deals]+)-([^/\.]+)$ index.php?main=$1&id=$2 [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?main=$1 [L]
Any help would be much appreciated.
Many thanks.
This code is faulty and is not going to work:
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
Reason is that RewriteCond %{HTTP_HOST} can only match host name. Replace that code with this:
RewriteRule ^home-Garden\ Clearance/?$ /services/garden-clearance-london [R=301,L,NC]