I have an old website and I had changed my CMS site to WordPress.
Now I have many error 404 records in my Google Webmaster log.
I want to redirect an URL to a new structure...
Old URL:
www.mysite.com/fa/contents/detailXXXX/postslug
where XXXX is a 4-digit number, e.g. 4256
New URL:
www.mysite.com/postslug
A very simple solution would be:
RewriteEngine On
RewriteRule ^fa/contents/detail[0-9]{4}/postslug$ /postslug [R=301,L]
However if you need to know what digits were in the URL and use it in your script, use a cookie:
RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug [CO=n:$1:.mysite.com:0:/,R,L]
...or use a query parameter:
RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug?n=$1 [QSA,R,L]
Related
I am trying to redirect the WordPress blog post from old to new domain.
We had a blog on subdomain http://blog.domain.xyz/ and after the migration is on main domain https://www.domain.xyz/
On the old blog, URL of the blog post was:
http://blog.domain.xyz/2020/03/25/post-name
(part /2020/03/25/ is just an example of date)
now I need it to redirect to:
https://www.domain.xyz/post-name
I matched with regex domain and date part:
http\:\/\/blog.domain.xyz\/\d{4}\/\d{2}\/\d{2}\/
I know how to redirect manually all posts one by one, but there are more than 1000 posts, so this is not an option.
I can't figure out how to take post-name part and apply it to the new domain
I think you need something like the following.
RewriteRule ^/\d+/\d+/\d+/(.*)$ https://www.domain.xyz/$1 [R=301,L]
// ^/ start at the root
// \d+/\d+/\d+/ match date folders like 2020/03/25/
// (.*) the part we want to keep "some-slug"
// $ end of match
// $1 put the part we want to keep here "some-slug"
Basically matching any urls with /2020/03/25/post-name and redirects to https://www.domain.xyz/post-name.
Note: this assumes you're adding the redirect at the old domain.
In the meantime I figured it out:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.domain.xyz$
RewriteRule \d{4}/\d{2}/\d{2}(.*)$ https://www.domain.xyz$1 [R=301,L]
</IfModule>
I'm rebuilding a website and changing a CMS. Former CMS had weird URL structure - it was adding post id in the end of the URL with forward slash. For example:
www.mydomain/category/someurl/54
I'd like to create 301 redirects in .htaccess with regex for a new CMS (WordPress). How could I create a pattern that would just simply match old CMS URL to new URL by removing last forward slash and id in URL?
Basically it should be like this:
301, from www.mydomain/category/someurl/54 to www.mydomain/category/someurl
You can place this rule as very first rule just below RewriteEngine line.
RewriteEngine On
RewriteRule ^(.+?)/[^/]+/?$ /$1 [L,R=301]
I have the following htaccess rule:
Redirect 301 / http://www.example.co.uk/blog/
On an old blog at http://blog.example.co.uk/ that should be redirecting ALL urls from this old blog to the new one.
However if I have anything in the path: e.g. http://blog.example.co.uk/2015/test-post then it redirects to http://www.example.co.uk/blog/2015/test-post
How do I make it so that it doesn't keep the path and just redirects to the domain.
You need to use RedirectMatch for that:
RedirectMatch 301 ^ http://www.example.co.uk/blog/
Make sure to test this in a new browser.
Or if you want to strip off any existing query string also then use mod_rewrite:
RewriteEngine On
RewriteRule ^ http://www.example.co.uk/blog/? [L,R=301]
I need to redirect a large amount of pages that have added random numbers onto the end of the page urls, I need to redirect these pages to a single url but there will be a large amount of urls throughout the website e.g.;
http://www.domain.com/a002-product-ring-50134.html
http://www.domain.com/a002-product-ring-50140.html
http://www.domain.com/a002-product-ring-50145.html
http://www.domain.com/a002-product-ring-50168.html
http://www.domain.com/a002-product-ring-50384.html
http://www.domain.com/a002-product-ring-50393.html
http://www.domain.com/a002-product-ring-50404.html
These need redirecting to http://www.domain.com/a002.html but the only thing i need to query is the number so something like this is what I thought would work;
Redirect 301 /a002-product-ring-(*).html http://www.domain.com/a002.html
You need to use RedirectMatch for regex capabilities:
RedirectMatch 301 (a.+?)-product-ring-[^.]+\.html$ http://www.domain.com/$1.html
We can use RewriteCond and match the uri pattern as following.
RewriteCond %{REQUEST_URI} ^.*(hello.html|contact_us.php)$
RewriteRule ^/(.*) http://www.domain.com/a002.html [R=302,L]
I am planning to relaunch a website soon. Part of the relaunch is a switchover from Joomla! to Wordpress.
Besides some old pages, which will be redirected with some exact expression, I have a lot of pages, which will be redirected to the new root page.
Whats the right regular expression, if I want to redirect URLs like these:
http://www.somedomain.com/index.php?somevar=123&com_option=somevalue
http://www.somedomain.com/index.php?com_option=someothervalue
All the URLs have in common:
http://www.somedomain.com/index.php?
+
com_option
How can I redirect all those pages with one line of redirect code in .htaccess?
I assume you do not need somevar and only want to carry over com_option
RewriteEngine on
RewriteBase /
RewriteRule index.php(.*)com_option=(.*)$ http://newsite.com/index.php?com_option=$2 [L,R=301]
You will need to do something like this:
RedirectMatch 301 (.*)/the/old/domain/and/url/(.*) http://the/new/domain/and/path/$2
Now, that the site is gone live, I see that none of the answers to my questions worked out.
After some trial an error I found a solution:
By now Google offers a live-demo:
1) Go to google & search for "site:sqlxpert.de".
2) Click on one off the links having "option" in the URL.
3) You will be redirected to the new pages root - which in this case is the same domain and also an index.php, but the d*** QUERY_STRING is gone.
I needed a .htaccess snippet, which does the following (expressed in words):
If requested file is
INDEX.PHP or INDEX2.PHP
AND
QUERY STRING contains "option"
then 301 REDIRECT to
http://www.sqlxpert.de/
WITHOUT any QUERY STRING
The solution:
RewriteCond %{QUERY_STRING} option
RewriteRule ^index\.php http://www.sqlxpert.de/$1? [L,R=301]
RewriteRule ^index2\.php http://www.sqlxpert.de/$1? [L,R=301]