I'm trying to remove a directory from any URLS that contain it. It's a wordpress blog.
Currently the following urls 404, and probably many others:
site.com/blog/archives/yyyy/mm/dd/
site.com/blog/archives/yyyy/mm/dd/post-title/
site.com/blog/archives/author/username
However, all of these urls work fine when they are in this format:
site.com/blog/yyyy/mm/dd/
site.com/blog/yyyy/mm/dd/post-title/
site.com/blog/author/username
So pretty much I need to take any url that has the director /archive/ in it, regardless of what comes after it, and remove it. I've tried lots of example online with no luck. I feel like I must be doing something wrong and not realizing it.
The last thing I tried was this, thinking it would be a very broad rule and work. No dice.
RewriteRule (.*)/archives/(.*) $1/$2 [NC]
Here is my current .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ([0-9]+)\.html /blog/?p=$1 [NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Right before the # BEGIN WordPress line add:
RewriteRule ^archives/(.*)$ $1 [L,R=301]
Related
am trying to get this simple RewriteRule to work:
RewriteEngine On
RewriteBase /
RewriteRule ^agb$ download/AGB.pdf$ [L]
RewriteRule ^impr$ impressum$ [L]
The file exists, and I can open it, if I manually open the url.
Now I have two problems with this.
The agb rule leads to a 404
The impr rule works (funny, as it has the same syntax as the agb rule), but it doesn't mask the url, it just changes it in the browser. Shouldn't the entered url stay the same, masked, not for the user to see, that there is some other URL behind it?
Many thanks.
*edit:
After following the advice to remove the $, I started playing around with the impr rule. Seems that the rule is not making the redirects, but wordpress is. Very try from /i over /imp to /impressu leads to the /impressum site. My bad, I didn't see this.
*edit:
The full file, as requested, looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^agb$ download/AGB.pdf$ [L]
RewriteRule ^impr$ impressum$ [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Okay - when reading the comment arkascha postet AGAIN AND COMPLETELY, it really were those $ on the target path.
Thanks a lot!!!!
Greetings and thank you in advance for your help!
I am trying to redirect certain subfolders from the "category" subfolder, but not all of them. Only about 5 of them.
Example:
http://domain.com/category/fish
http://domain.com/category/lamb
Should redirect to
http://domain.com/tasty/fish
http://domain.com/tasty/lamb
But:
http://domain.com/category/lead-paint
Should not.
What I've tried (in .htaccess):
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
I tried both before and after the Wordpress module. I also tried inserting inside the Wordpress module like so:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Changes to .htaccess definately work. Typos generate a 500 response, and I can block by IP address. I'm not sure if Wordpress is overriding my redirects, or if I have a syntax error.
Thank you again.
You need to remove the leading slash just after ^ in the two rules. You also don't need to specify the full domain. The following will do
RewriteRule ^category/fish/?$ /tasty/fish [R=302,L]
(Note the 302 - always test with a temporary redirect, then change to permanent when you're happy.)
I am helping out with a website that has a main/root website as well as a WordPress installation in a sub-directory. The .htaccess file of the main site has rules to redirect non-www traffic to www:
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/$1 [R=301,L]
However, this has no effect on the WP site. For instance:
site.com would redirect to www.site.com
site.com/some-content-belonging-to-root-site would redirect to www.site.com/some-content...
site.com/wordpress does not redirect to www.site.com/wordpress
I added similar rules to the WordPress .htaccess, playing around to try to get the result I would expect, but the best I could come up with is this:
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
...which successfully redirects site.com/wordpress to www.site.com/wordpress, but does not redirect site.com/wordpress/a-blog-post.
I feel like I must be missing something simple or obvious, but hours of experimentation and research have not yielded a thing except that .htaccess files in sub-directories are not advised (but I think this is unavoidable in this case).
I would be grateful for any suggestions on how to fix the rewrite condition or rule to successfully redirect the post links of the WP site.
Thanks!
EDIT / ADDITIONAL INFO: I modified the RewriteCond to better match the incoming URLs, thinking that this might help, but it has the same effect. The base wordpress URL is rewritten, but article URLs are not:
RewriteCond %{HTTP_HOST}/wordpress/.* ^site\.com/wordpress/.* [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
I wouldn't think that it should matter, but just in case, I'll mention that the article URLs look like this:
site.com/wordpress/2014/05/16/article-slug
It turned out that the problem was not the rewrite conditions or rules, but the order in which they appeared in the .htaccess file.
I read a WP forum post in which someone suggested moving the rewrites to the top of the file for performance reasons. I gave it a shot just to see what would happen and noticed that the URL for the articles was affected - not in a good way, but affected nonetheless. I got something like this:
www.site.com/wordpressindex.php?/2014/05/16/article-slug
There were some rewrite rules to rewrite the base and redirect to the index given a nonexistent directory or file, which was what was building the garbled URL above.
In case it happens to help anyone else, I began with a .htaccess file that looked like this:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
And ended up rearranging it to look like this, which now works to properly redirect all blog-related URLs to the www site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{HTTP_HOST}/wordpress ^site\.com/wordpress [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Try:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
And clean your browser history to make sure.
I also own a WordPress website and I was looking for www to non www redirect for my website : fasterthemes.com
The website is hosted in DigitalOcean and after struggling with several DAYS, I could figure out that all the solutions (.htaccess) codes available work fine if you've set the A record in your DNS settings properly. If it's not set then the redirection doesn't work !
So in my case,
I created one A record in my Digitalocean DNS like below :
i.stack.imgur.com/ie7f8.png
I added following code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.)$ http://%1/$1 [R=301,L]
And now as you can see if you browse http://www.fasterthemes.com , it redirects you to fasterthemes.com. Earlier it was throwing an error.
I hope this helps.
I have a problem redirecting links using htaccess rewrite. Originally I had only the Rewrite Rule part without the WordPress code underneath and it was working fine. Now, after I tried to add a permalink using Wordpress, the link is giving me a redirect loop problem.
Essentially what I want to do is allow the viewers to go straight to 'www.example.com/blog' when they type 'www.example.com'
I think I am missing something because I thought that the rewriteRule would stop the moment
RewriteRule ^$ /blog [R=301,L]
is processed because of the [L] flag and then other RewriteRules would not be looked at. Can someone help me understand how the server actually go through the steps of the code below?
RewriteEngine on
RewriteRule ^$ /blog [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
You need to put the line:
RewriteRule ^$ /blog [R=301,L]
In the htaccess file in your document root, not in the /blog directory.
So, I'm trying to add my own RewriteRule in addition to what Wordpress already has. A little backgroun. The site I am working on is built off of a custom CMS, but also has a blog that is powered by WordPress. So, heres what I have thus far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ /locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I also got rid of a couple lines of the Wordpress Rewrite code that seemed unecessary (everything still works....I think). But yeah, it seems like it is overwriting the rule that I wrote. Help?
Oh, also, I just tried to get rid of the . that is before /blog/index.php [L] and replaced it with just a ^, it worked...kind of. It loaded the blog when I went to /blog/, but when I went to something like /blog/author/admin, I got a 404.
I think you need to add the last flag [L] after your new rule, otherwise processing will continue and your request will be rewritten to '/blog/index.php'.
RewriteRule ^locations/(.*)$ /locations2.php?info=$1 [L]
Have you tried to remove the RewriteBase / and the slash before locations2.php ?
Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Custom rewrite for locatons
RewriteRule ^locations/(.*)$ locations2.php?info=$1
# Wordpress rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
If the blog gets unreacheable, move RewriteBase / to after the custom rewrite. Also, I believe that if the blog is in a sub-directory, the RewriteBase should be RewriteBase /blog/