htaccess redirect to old website - regex

I changed my whole website with a new once, but now I don't want to lose all the traffic that is coming from google. So i changed the old websites folder to www.domain.com/old/ and there are all of my articles.
How can i redirect from htaccess all the error 404 links on the new website to the old website.
Example:
The link request is www.domain.com/article29384.html and it causes error 404, now i want the htaccess to redirect it to www.domain.com/old/article29384.html
Solution: (thanks to anubhava)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/old/ [NC]
RewriteRule ^(.+?)$ /old/$1 [NC,L,R=301]

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/old/ [NC]
RewriteRule ^(.+?\.html)$ /old/$1 [NC,L,R=301]

Related

Htaccess rewritting non www to www /and/ domain.tld/id to domaine.tld/page.php?id=

I can't find solution to do 2 things on my htaccess for my domain :
1/ -> Redirect https://domain.tld to https://www.domaine.tld (index.php)
2/ -> Url rewritting to have url like https://www.domaine.tld/id witch call https://www.domain.tld/page.php?id=
My htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ https://www.domain.tld/$1 [L,R=301]
# Redirection page.php?id=
RewriteRule ^([0-9a-zA-Z\-]+?)?$ /page.php?id=$1 [L]
With this htaccess I'm redirected to page.php?id=$1 but if I try to go on index I'm always redirected to page.php
I know the 2 conditions are the same but I don't know how could I fix it.
An idea ?
Thank you :)
With your shown attempts, please try following rules in htaccess file. Make sure your htaccess file is residing along with index.php, page.php files.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect with www attached in url.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##Internal rewrite for home pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?$ index.php [QSA,L]
##Internal rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ page.php=$1 [QSA,L]

Apache .htaccess "Redirect to non-url" error

I've not used URL rewriting on .htaccess for a while, and now I think I've forgot some of the basis.
This is my server directory structure:
https://www.domain.com/dir/
In which I've put my website, with those files:
index.php
list.php
edit.php
home.php
What I wanted to achieve is this: User writes url like https://www.domain.com/list and .htaccess sends that request to index.php?section=list.
So, this is my current .htaccess:
RewriteEngine On
RewriteBase /dir
Redirect on HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule /([a-zA-Z]+)/$ /index.php?section=$1 [NC,L]
Testing it on htaccess tester seems to work as expected, but not on my webserver.
I've got no more .htaccess files in the server root dir, and by viewing server logs I only get Redirect to non-URL errors.
I've also seen this answer but it didn't help me so far.
Have this .htaccess in /dir/ directory:
RewriteEngine On
RewriteBase /dir/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [QSA,L]
This will support pretty URL as http://domain.com/dir/list

301 Redirect All Blog Posts To New Subfolder

I am currently using this .htaccess rewrite to get pretty urls for my blog posts:
RewriteRule ^([a-zA-Z0-9-]+)$ /item.php?slug=$1 [L]
This ends up looking like:
example.com/my-new-awesome-blog-post
What I would like is to move all my posts to a new folder called items so that it would look like:
example.com/items/my-new-awesome-blog-post
I not only want to move all blog posts to the subfolder, but also to ensure it is a proper 301 redirect so there is no issue with SEO.
Thank you for your help/advice.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+items/ [NC]
RewriteRule ^ items%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^items/([a-zA-Z0-9-]+)/?$ items/item.php?slug=$1 [L,QSA]

301 redirect www.root.com/subfolder to www.subfolder.com on same server

Hi people at Stackoverflow,
I've honestly searched here and tried stuff but since I'm really not an expert and nothing worked, I'm lost and need your help. I don't post here often because usually I find what I am looking for.
The situation
I have different sites running on the same server. Let's say that one of my websites is called Cats. It runs with CMSMS and resides in a subfolder of my root called cats.com.
In the root of my server there is a htaccess file with this code (generated by the server admin):
RewriteCond %{HTTP_HOST} ^www.cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
RewriteCond %{HTTP_HOST} ^cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
In the cats.com subfolder there is a htaccess file with rewrite code for pretty urls:
# RewriteBase /cats.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
This works. When I request www.cats.com I see the contents of the /cats.com subfolder in my root but the URL base stays www.cats.com and all the URLS are extremely pretty.
However, due to a setup error (my bad) in my site's config file, the URL listed in Google is
http://www.root.com/cats.com
When I click the URL, I see the correct page but the address bar reads http://www.root.com/cats.com. The pretty URL rewrite works, but I want this url to rewrite to www.cats.com.
What I have tried
I have tried rewriting the Google listed URL with
attempt one
I tried this in both htaccess files, below and above the existing rewrite rules.
RewriteRule ^/cats.com(.*)$ http://www.cats.com [R=301,L]
RewriteRule ^/cats.com/(.*)$ http://www.cats.com/$1 [R=301,L]
Nothing happens. Everything stays the same.
attempt two
Again tried this in both htaccess files, below and above existing rewrite rules
Redirect 301 /cats.com http://cats.com
This results in an infinite loop in all occasions.
I hope my description is clear enough...
If anybody has any idea what might / should work I'd love to hear it.
Thank you for your help!
Change your rules in cats.com subfolder's htaccess file to this:
RewriteEngine On
RewriteBase /cats.com/
RewriteCond %{THE_REQUEST} /+(cats\.com)/(\S+) [NC]
RewriteRule ^ http://%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

.htaccess clean URLs with slugs 404 not found

I apologize for asking this question once again, but I'm unable to make my URLs work without getting an 404 page error.
public_html
.htaccess
webroot+
index.php
article.php
I'm using htaccess to make the webroot folder the main document root ( I do not have access to virtual hosts)
RewriteCond %{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
I'm not sure if the issue is caused due to the code above but when I try making my URLs clean for seo I get an 404 page.
I'm trying to change www.site.com/article.php?article_uid=number&article_title=title ---> www.site.com/article/id/title/
With the following code it allows the page to change the url but it gives me an 404 not found.
RewriteCond %{THE_REQUEST} \ /article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=301,NE]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^article/([^/]+)/([^/]+)/?$ /article.php?article_uid=$1&article_title=$2 [L,QSA]
ex.
The requested URL /webroot/article/id/title/ was not found on this server.
Change last rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ webroot/article.php?article_uid=$1&article_title=$2 [L,QSA]