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]
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 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]
i've moved an old site to wordpress and now i need to redirect the old links to new one
The old url
mysite.com/2010-11-02-11-05-12/category-old/subcategory/123-article
Here are my redirects:
to category page (working)
RedirectPermanent /2010-11-02-11-05-12/category-old/ /archive/category-new/
to article page (not working)
RedirectPermanent /2010-11-02-11-05-12/(.+?)/^[0-9]+-(.+?)/?$ /$1 [L,R=301]
redirect url (404)
mysite.com/archive/tag/subcategory/123-article
the url should look like this (the number before are removed 123-):
mysite.com/article
could anyone help?
Change it to:
RewriteEngine on
RedirectRule 2010-11-02-11-05-12/(.+?)/[0-9]+-(.+?)/?$ /$1 [L,R=301]
Which seems to be what you were trying to use. I removed the opening forward slash as they are not included in a match from .htaccess. I removed the caret (^) from before the numbers as that would stop it matching. Let me know any problems.
I'm literally at the end of my tether with this. I've researched many other questions and answers on stackoverflow but still can't find the solution i need. I'm starting to think what i want to do is not possible.
So... the problem is this. I want to turn the following:
e.g.
www.mydomain.com/visa-information/country.php?country={COUNTRY-NAME}&passport={PASSPORT-NAME}
To the following pretty url:
www.mydomain.com/visa-information/{COUNTRY-NAME}-visa-for-{PASSPORT-NAME}-citizens/
I have a partially successful rule in my htacces file as so:
RewriteRule ^/visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$ visa-information/country.php?country=$1&passport=$2 [NC]
which works fine and does what i want if i enter the url into the browser address bar, but the real problem i'm having is getting it to re-direct to the pretty url via a form i have on pretty much every page of the site.
I've tried various re-direct rules like the one below:
RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+) [NC]
RewriteRule visa-information/country.php visa-information/%1-visa-for-%2-citizens/? [R,NC,L]
But no luck. I've also tried adding the QSA flag to the above re-direct rule, but it just ends up with an endless loop.
I have tried using a location php re-direct header at the top of the country.php page to re-direct after form submission like so:
if(isset($_GET['country']) && isset($_GET['passport'])) {
header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();
}
I was expecting the above to work like entering the pretty url directly into the browser works, but it doesn't, just gives me a 404 error.
Any help is greatly appreciated.
Thanks
Jordash
EDIT
My local directory structure is as follows:
/webserver/mydomain.com/visa-information/etc...
On the live server it will be:
mydomain.com/visa-information/etc..
As i am using an Apache Alias on my local machine i have set RewriteBase as:
RewriteBase /webserver/mydomain.com/
I currently have the following set of RewriteRules adapted from what anubhava gave me:
RewriteCond %{REQUEST_URI} visa-information/country.php [NC]
RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+) [NC]
RewriteRule visa-information/ visa-information/%1-visa-for-%2-citizens/? [R=301,L,QSA]
# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$ visa-information/country.php?country=$1&passport=$2 [NC,L]
This currently gives me an endless re-direct loop, both when entering the pretty url in the browser bar, and when using my form, however if i disable the top 3 rules then i find i can enter the pretty url into the address bar and the rewrite works, but not from the form submission of course.
I really don't know what i'm doing wrong. Why is there an endless loop?
Not sure how first rule is working for you since RewriteRule doesn't match leading slash in .htaccess. Also to redirect old url to pretty URL you need to use THE_REQUEST variable that represents original request received by Apache from your browser. Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from old URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+visa-information/country\.php\?country=([^\s&]+)&passport=([^\s&]+)\s [NC]
RewriteRule ^ /visa-information/%1-visa-for-%2-citizens/? [R=301,L]
# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([a-z-]+)-visa-for-([a-z-]+)-citizens/?$ /visa-information/country.php?country=$1&passport=$2 [NC,L,QSA]
Looks like the issue was actually with my php form and not the mod_Rewrite rules. The form "action" was pointing to the same form page (as in $SERVER['PHP_SELF']) which works fine without the rewrite rules, but causes an endless loop when they are activated.
I simply made a search_action.php page and then redirect the form there using a php header to the pretty url:
header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();
The mod_rewrite rule I had originally works fine and now the user can get to the desired page with the pretty url from the form or by tyoing directly into the browser address bar.
I'm not sure it's actually possible to action a form to the same page whilst rewriting the query string, without using an intermediary action page from the form, or Javascript. I'm sure many of the more experienced programmers will have known this already, but not me unfortunately.
I'm currently consolidating posts on a site we recently acquired that had multiple WordPress installs to manage content, one in the public_html folder and another in a subdirectory, like so:
1. http://domain.com/
2. http://domain.com/another-install/
We're moving all of the content from /another-install/ into the main setup, and using a 301 redirect to remove /another-install/ from all old links like so:
RedirectMatch 301 ^/another-install/(.*) http://domain.com/$1
Resulting in all articles redirecting like so:
http://domain.com/another-install/article-name/
TO
http://domain.com/article-name/
The problem is, we want to keep /another-install/ viewable as a page. With the current redirect, http://domain.com/another-install/ goes to http://domain.com/. Is there any way to add an exception, or rewrite the current rule so that it keeps /another-install/ viewable?
Change your regex from (.*) (which matches 0 or more of any character) to (.+) (which matches 1 or more of any character). That means there would have to be something following /another-install/ in order for there to be a redirect.
You need a RewriteRule to specify exclusions. Add this to your .htaccess file
RewriteCond %{REQUEST_URI} !^/old-install/(index\.wml)?$ [NC]
RewriteRule ^old-install/(.+)$ http://domain.com/$1 [R=301,NC,L]