mod_rewrite: insert index.php if URL contains X - wordpress

I'm trying to use mod_rewrite to change:
http://example.com/?wpdmact=process&did=m5d4FG3
to:
http://example.com/index.php?wpdmact=process&did=m5d4FG3
I just want to insert index.php.
Here is the code I have tried, but that is not working:
RewriteCond %{QUERY_STRING} wpdmact=process&did=(.*)
RewriteRule (.*) /index.php?wpdmact=process&did=%1 [R=301,L]
I have successfully created the following similar redirect on this same site:
RewriteCond %{QUERY_STRING} page_id=(.*)&preview=true
RewriteRule (.*) /index.php?page_id=%1 [R=301,L]
I have a Wordpress site that has a static index.html in the root folder along with index.php. This is to have a lighter, faster loading homepage. The only challenges this has created are the two I'm mentioning here. It breaks because in these instances the site redirects to index.html, instead of index.php.
I don't do a lot of work with mod_rewrite, so any help you can offer is greatly appreciated.

This should work:
RewriteCond %{QUERY_STRING} wpdmact=process&did= [NC]
RewriteRule ^$ /index.php [R=301,L]
QUERY_STRING is automatically carried over to new URL.

Related

Single URL mod_rewrite keeps going back to the original URL

I am trying to do a very simple single URL rewrite.
This is it:
RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog.
I have tried a number of different versions of the rewrite but all I have achieved thus far is that I don't get a 404 on example.com/blog but keeps going back to example.com/?post_type=post.
I have placed RewriteRule right at the top of the .htaccess file which didn't help.
These are the other rewrites I have in the same .htaccess file:
#Single URL
RewriteRule ^blog ?post_type=post [NC,L]
#http www rewrite
RewriteCond %{HTTP_HOST} ^instrumentrentalbarcelona.com [NC]
RewriteRule ^(.*)$ https://www.instrumentrentalbarcelona.com/$1 [L,R=301,NC]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thank you #DocRoot for helping me with this. The answer is detailed in the comment thread below by DocRoot and I. Just thought I would summarise this.
I wanted to show a page with all my blog posts.
This can be done if you use the WordPress post type "Post". example.com/?post_type=post
I wanted that page to show when you go to example.com/blog.
The following Rewrite works to show the desired URL: (thank you again #DocRoot)
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
But /blog was giving a 404. So;
Create a page in WordPress called "blog"
Go to: Setting > Reading > Your homepage displays > Posts page: [Select page "blog"]
Save and done.
RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog
The directive you posted does the exact opposite. It "rewrites" (not "redirects") from /blog<anything> to /?post_type=post
To redirect based on the query string you need an additional condition that checks against the QUERY_STRING server variable because the URL that is matched by the RewriteRule pattern is the URL-path only.
Try the following instead:
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
This "redirects" from example.com/?post_type=post (exactly) to example.com/blog.
The QSD flag (Apache 2.4+) is required to remove the query string from the redirected request. If you are still on Apache 2.2 then you will need to append the substition with ? instead of using the QSD flag. eg. RewriteRule ^$ /blog? [R,L].
Not also, this is a temporary (302) redirect. If this should be permanent, then change the R flag to read R=301 - but only once you have confirmed this is working OK.

How to 301 redirect all other URLs that do not meet conditions using htaccess

I am doing bunch of 301 redirects via the htaccess file for a WordPress website. I have listed about 80 specific URLs/directories that point to their new relative position on a new domain.
However, there are still a bunch of URLs that are lurking around, that I want to redirect to the new domains home page.
What I am trying to do is if the URL does not meet any of the hardcoded URLs, redirect it to the home page.
Here is what I have:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Begin Redirects
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/14/14/someother\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]
# End Redirects
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The code that I am using to catch all other URLs and send to the home page is written as:
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]
The issue I am having is that it seems to jump straight to that condition, redirecting all the URLs to the home page, whether that piece of code is at the top of the htaccess file or the bottom. Is there a way to set this as a condition so it only redirects the URL if it does not meet any of the URLs or directories above it?
If I remember correctly, RewriteRule can't be used like that, which normally we'll write it like:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteCond %{REQUEST_URI} ^/2015/12/14/some-directory/
RewriteRule (.*) https://www.newdomain.com/? [L,R=301]
Where your code is:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]
Also, you could test your .htaccess with this tool first which can save your time from transferring file between server and clearing cache (if you need).
Second, I don't really see the meaning of your code, since all URLs from www.olddomain.com will be redirected to https://www.newdomain.com/?. Unless you meant, redirect base on the path but not the domain, which you'll need to remove the first line of my code.
Last, notice I didn't add $ behind the path, since I'm not really sure if you need a wildcard redirect which /2015/12/14/some-directory/something also redirects to https://www.newdomain.com/?. If you don't, remember to add it back.

Wordpress - moving a site to another domain, 301 redirects

I moved a wordpress site to another domain. I want to preserve the google links however and help it re-index the new site.
I did a 301 redirect but it only works for the root director. When someone goes to http://oldsite.com/directory/post-name-here/ I want them to be forwarded to http://newsite.com/directory2/post-name-here/. Both websites are in subdirectories. Is there a way to do it with in PHP? Or would I need a mod-rewrite?
What finally worked for me was this:
My oldsite file structure is like so:
/public_html/
index.php
/development/
.htaccess
To recap, I wanted to forward everything coming into the development directory to the new website. This is the code of the .htaccess in the development directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.org$
RewriteRule (.*)$ http://newsite.com/newdirectory/$1 [R=301,L]
Thank you so much to Peter
This has always worked for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
You have to add this rule to .htaccess file see below.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
If you have only one link under the directory. You can add a index.php file to http://oldsite.com/directory/ with this code.
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
If you have many links under the directory, then use the following code
if($_SERVER["REQUEST_URI"]=="/directory/post-name-here/"){
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
}
Here, we check if the file name is the old link, then redirect to new domain.

How to use .htaccess to remove '?m=1' in the end of url?

I just moved my blog from Blogger to WordPress, and have a problem with the mobile URL.
WordpPress have a function to setup URL structure, so the URL for desktop is OK, but there is an additional ?m=1 in mobile version Blogger URL.
This is what I'm trying to do:
redirect http://www.example.com/2016/05/artical.html?m=1
to http://www.example.com/2016/05/artical.html
I tried this but it didn't work:
RewriteCond %{QUERY_STRING} ^(.*)(^|&)m=1(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
Hope this helps just after RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]

htaccess subdomain redirect and worpdress

After setting up Wordpress to use permalinks (which added some lines to my htaccess file), I got confused how to redirect a subdomain to a subfolder (astro.aspiracoesquimicas.net should redirect to aspiracoesquimicas.net/astro): what I had tried (and worked) before doesn't work anymore:
RewriteCond %{HTTP_HOST} ^astro\.aspiracoesquimicas\.net$
RewriteCond %{REQUEST_URI} !^/astro/
RewriteRule (.*) /astro/$1
Now I get a 500 Internal Server Error with the subdomain. I don't know about htaccess configuring and don't understand what changed because of Wordpress.
I'd also like to redirect blog.aspiracoesquimicas.net to the main domain aspiracoesquimicas.net
How can I do it?
Make sure you add the L flag to the rule and make sure you place the rules BEFORE the wordpress rules:
RewriteCond %{HTTP_HOST} ^astro\.aspiracoesquimicas\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/astro/
RewriteRule (.*) /astro/$1 [L]
RewriteEngine On .
RewriteCond %{HTTP_HOST} !^astro\.aspiracoesquimicas\.net/
ReWriteRule ^(.*)$ https://aspiracoesquimicas.net/astro/$1 [R=301,L]
use this method

Resources