Why are .htaccess RSS rules being ignored? - wordpress

I'm in the process of migrating a blog to a new platform & server, and having trouble with mod_rewrite .htaccess rules. So far I'm able to redirect post URLs and the root domain the the new server, but the rules for the RSS URL is being ignored.
Here are my rules:
RewriteRule ^[0-9]+/[0-9]+/([^/]+)/?$ http://blog.example.com/$1 [R=302,L] #working
RewriteRule ^/rss$ http://blog.example.com/rss [R=302,L] #not working
RewriteRule ^$ http://blog.example.com/ [R=302,L] #working
The first and last rule are working as expected, but the second rule is not redirecting. If I type in http://example.com/rss it does not redirect to http://blog.example.com/rss
I feel like I'm missing something simple. This is my first time fiddling with mod_rewrite. Thanks.

Assuming that you're using apache 2.0+, you need to remove the leading slash from the patterns because they get stripped by apache when rules in an htaccess file are being applied.
RewriteRule ^rss$ http://blog.example.com/rss [R=302,L]

Related

Apache HTAccess Remove Query String for redirect

It sounds like a relatively simple one but here goes.. I have a wordpress website called example.com It has recently been redeveloped using wordpress however the old sites structure used the following www.example.com/?page_id=22 I want to redirect anything after the domain name that contains /?page_id=[any digit] to the homepage, im having great difficulty in accomplishing this and I also think I may have issues because wordpress itself uses the query string page_id before the permalinks redirects kick in, I could be wrong though but any help on removing the query string directly after a domain name would be great especially keeping in mind that it is a wordpress install.
I have tried various iterations that look like the following to no avail
RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule (.*) http://www.example.com/ [R=301,L]
RedirectMatch 301 ^/page_id=(.*)$ /
Try this rule right at top, just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^page_id=\d+$ [NC]
RewriteRule ^/?$ /? [R=301,L]
/? will strip off any query string.

redirect a domain extension to a subdirectory

I am trying to create a permanent htaccess redirect (301) from all my domain extensions into the appropriate subdirectories. The "rules" are as follow:
Redirect belgian website to its subdirectory on the main website:
from: www.example.be
to: www.example.com/befr/
Of course I would like to preserve the url parameters (if any) of the "from". Globaly, if someone entered the first url it should redirect to the second url (langage subdirectory in the main website).
I'm using wordpress and I'm hosting on a plesk I've read many things here but I'm stuck, thank you very much in advance for your help
PS: I've tried that but it doesn't work
RewriteCond %{HTTP_HOST} ^(www\.)?example.be$ [NC]
RewriteRule ^(.*) http://www.example.com/befr/$1 [L,R]
After reading your question, your code should be working (with informations you gave).
If it's not, here are some points to check:
1. Make sure mod_rewrite is enabled and htaccess can be executed (Apache config).
2. Your htaccess has to be in root folder (where example.be is forwarded).
3. About your htaccess' code:
since you're using Wordpress, make sure your rule is on top of other rules
don't forget to escape . (second one) in RewriteCond (otherwise it doesn't mean the same) even if it works that way
replace R flag (302 by default) by R=301 if you want a 301 redirect
Your code now looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.be$ [NC]
RewriteRule ^(.*)$ http://www.example.com/befr/$1 [R=301,L]
# your other rules (and Wordpress' default rule) here

Using htaccess to rewrite special characters for Wordpress url aliases

I recently migrated a fairly large site (~6,000 posts) from Drupal to Wordpress. As part of the process, I migrated the Drupal-created url aliases to Wordpress for SEO and link retention purposes.
An example of a url alias that Drupal created that worked great in Drupal:
/stories/will-this-be-another-la-niña-year
That url in Wordpress returns a 404. However, this works:
/stories/will-this-be-another-la-nina-year
It seems then my best bet is to write a generic international character to english character set rewrite rule in htaccess, before the url is passed to Wordpress.
Any idea how I might do this?
Thanks a lot for whatever help you can give.
Matt.
It seems like there might be a better way to do this within wordpress, you may want to do a quick browser through the wordpress Trac tickets, there maybe some patch or temporary fix for the problem. But if you need to go to an htaccess/redirect method, you can either use a RewriteMap to sanitize and redirect-if-needed or explicitly redirect on non-ascii characters.
A RewriteMap requires access to either server or vhost config to setup the map. It could be as simple as a list of /stories/will-this-be-another-la-niña-year URIs mapped to http://yourdomain.com/stories/will-this-be-another-la-nina-year (the all ascii URL, the http:// is significant because it tells mod_rewrite to redirect the browser). Or you can write a script to look for non-ascii characters and replace them with the appropriate ascii character.
Text mapping:
RewriteMap sanitize txt:/path/to/uri_mapping.txt
Script mapping:
RewriteMap sanitize prg:/path/to/sanitize_script.php
Then in your htaccess file, you can invoke this mapping like this (these rules will need to be above the wordpress rules, since you want the URI sanitized before wordpress gets a hold of them.
RewriteRule ^(.*)$ /${sanitize:$1|$1} [L]
If you don't have access to server/vhost config, you'll have to enumerate the possibilities in your htaccess file, again putting these rules above the wordpress rules:
# replace ñ
RewriteRule ^(.*)ñ(.*)$ /$1n$2 [R=301,L]
# replace ú
RewriteRule ^(.*)ú(.*)$ /$1ú$2 [R=301,L]
etc.
I just added the following lines at the beginning of my .htaccess file and it works:
RewriteRule ^(.*)é(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)è(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)ê(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)î(.*)$ /$1i$2 [R=301,L]
RewriteRule ^(.*)ô(.*)$ /$1o$2 [R=301,L]
RewriteRule ^(.*)û(.*)$ /$1u$2 [R=301,L]
RewriteRule ^(.*)â(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)à(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)ï(.*)$ /$1i$2 [R=301,L]

How do I modify my .htaccess file to redirect a URL for one domain only, and not rewrite other domains hosted by the same server

I switched blogging software (MT to WordPress) on a site and need to redirect requests to http://www.domain1/atom.xml to http://www.domain1.com/feed/atom.
I was using a simple Redirectmatch rule, but realized that it was also redirecting requests made to another site (domain2), that is is hosted by the server, in a subdirectory of domain1, which I do not want to happen (its feed is still at http://www.domain2.com/atom.xml).
How do I get the redirect to only occur for domain1?
I was trying to do the following, but it didn't work.
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^/atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
Am I close?
Thanks,
Rich
If you don't do any rewriting for domain2 then a quick fix would be to create a .htaccess file inside its root folder and disable rewriting with RewriteEngine off.
Otherwise you are on the right path with the RewriteCond, it should do the trick. Have you tried adding $ at the end (RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]) / any misspelling / www. vs no www.?
I figured it out, but I'm not sure why exactly this works. I moved my .htaccess to be:
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
I removed the slash in front of "atom" in the RewriteRule.
I would think I should have the slash, as I'm tyring to redirect http://www.domain1.com/atom.xml .
It's at the root of the domain...
Oh well. Can anyone explain why this works? Is the string passed to the pattern matching not contain the starting slash?
Thanks,
Rich

htaccess url rewriting question

I have a site made with CodeIgniter with a WordPress site at /blog.
Say I create a page in WordPress that can be viewed at /blog/my-page.
Is it possible to rewrite the URL with .htaccess to remove the blog part of the URL? So I could enter my site url /my-page to view the page?
from the top of my head..
#start the engine
RewriteEngine on
#requests to www.yourpage.com/anything/
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$ [NC]
#are sent to www.yourpage.com/blog/anything/
RewriteRule .* /blog/%1 [L]
The rule below will rewrite (internal redirect) /my-page to /blog/my-page:
RewriteEngine On
RewriteBase /
RewriteRule ^my-page$ /blog/my-page [NC,L]
RewriteRule ^another-page$ /blog/another-page [NC,L]
This needs to be placed in .htaccess in website root folder.
If you already have some rewrite rules there then this one need to be placed in appropriate place as order of rules matters.
You still may need configure WordPress a bit so it understands that this URL is for him to process (WordPress may still see the original URL). I have not worked with WordPress that much to tell if this will be required (and how to do it if it is) -- but look at Permalinks settings.

Resources