htaccess Redirects with Wordpress - wordpress

I am working with a GoDaddy Hosted Wordpress website.
I have users that will attempt to go to <mywebsite.com>/jobs and I need them to be redirected to <mywebsite.com>/newpath/corporate-opportunities/.
I am under the impression that I can place rewrite rules in the .htaccess file that exists at the root of my GoDaddy Hosted installation. I have the following in place:
# 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
<IfModule mod_rewrite>
RewriteEngine on
RewriteRule http://<www.mywebsite.com>/jobs http://<www.mywebsite.com>/newpath/corporate-opportunities/ [R]
</IfModule>
The first set of rules (within the Wordpress Begin and End comments) is what is commonly put in place for the Wordpress 'permalink' URL rewrites so they are pretty. The rewrite rule after the Wordpress block is how I am attempting to do the redirection.
I am not able to get it to work.

You need to move your redirect rules above the WordPress rewriting ruleset, so that they are seen before-hand. Additionally, the RewriteRule pattern (the first part) is always the request URI, and not the fully-qualified URL - as you are working with one domain, you also do not need to use the fully-qualified URL in the substitution part. Lastly, you need the L flag to stop processing at that point.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/?$ /newpath/corporate-opportunities/ [R,L]
</IfModule>
# 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

Related

Static redirection in .htaccess with rewriterule

I should make some url redirections (no variables to pass) in .htaccess but I don't want to let see the destination url. How can I make?
I've already tried to use Redirection instruction like this:
Redirect /en/folder1/url1 /en/folder2/url2
But in this way the destination url is visible. So I started to try to use RewriteRule like in this example:
RewriteRule ^/en/folder1/url1$ /en/folder2/url2
but I've always error page as result.
P.S. This code is inside a .htaccess file with the default Wordpress url rewrite code.
EDIT. This is the complete .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^en/folder1/url1$ en/folder2/url2
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to remove the leading slash from your Rule's pattern
RewriteEngine on
RewriteRule ^en/folder1/url1$ /en/folder2/url2 [L]

Redirect wordpress uploads folder to s3 bucket using .htaccess

I'm trying to redirect my uploads folder to my s3 bucket using htaccess in wordpress however it's not working. All the images on my site are still getting served locally from the server instead of linking to the bucket.
This is my htaccess file:
# 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
RewriteEngine On
RewriteRule ^wp-content/uploads/(.*)$ https://BUCKET.s3.amazonaws.com/wp-content/uploads/$1 [L]
# BEGIN W3TC CDN
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
# END W3TC CDN
There are a couple of problems. You need to put the rule ABOVE your wordpress rule because wordpress rules route every request to index.php.
Next you will need to use mod_proxy with the use of the [P] flag because regardless of not specifying redirect in the rewriterule, Apache will do it anyway because the substitution URL is a new domain. So Apache will perform a Redirect. So using [P] flag should proxy the content instead of doing a redirect. This should accomplish what you need.
RewriteEngine On
RewriteRule ^wp-content/uploads/(.*)$ https://BUCKET.s3.amazonaws.com/wp-content/uploads/$1 [P]
# 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

htaccess not working OK

I have below code inside my .htaccess which is placed inside my wordpress blog located under subdomain. Now I moved my blog under subfolder and I placed 301 redirects from subdomain but when I visit pages at blog.domain.com I get 500 internal server error instead of getting 301 redirects to http://domain.com/blog/
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
RewriteCond %{HTTP_HOST} ^blog\.stream-tek\.com
RewriteRule ^(.*)$ http://www.stream-tek.com/blog/$1 [R=301]
# 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
Now when I remove code between:
# BEGIN WordPress
# END WordPress
I get 301 redirects from subdomain pages, but when I return it back its not working. I need that part of code since wordpress clean urls wont work without that.
What is the problem with above htaccess file since I am not expert with that.
Thanks for help.
Problem is the WP stuff take full control even before your redirect gets a chance to kick in.
Change the order of rules.
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
RewriteEngine On
RewriteBase /blog/
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^blog\.stream-tek\.com$ [NC]
RewriteRule ^ http://www.stream-tek.com/blog%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

Rewriting URL from postNuke to Wordpress

I have a postNuke site that I'm currently transferring to Wordpress and I would like the format of the old site not to be lost in the conversion. I've tried to play with it but I'm going nowhere fast!
The current site currently rewrites the URL to :
domain.com/Article1234.html
where 1234 is the internal id number of the article.
The ID in the old site is the same in the new site. The URL in the new wordpress site currently rewrites to a custom structure :
/%category%/%postname%
Here is what is in the .htaccess file:
# 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
What is needed to be done to redirect the old format to the new one? Change rewrite rule in htaccess? 301 redirect? wordpress plugin for rewriting?
If you don't have a lot of articles, you can use RedirectPermanent, but you have to write yourself the directives in your .htaccess file.
Or you can use .htaccess like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^Article([0-9]+)\.html$ /index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This will rewrite Article1234.html to index.php?p=1234, then wordpress will make a 301 redirection.
Be careful when modifying permalinks since .htaccess could be override, to avoid this you can place custom rewrite rules before # BEGIN WordPress.

How do I add additional mod_rewrite rules on a wordpress site?

I have uploaded a file 'events.php' to my wordpress site (the wordpress is installed in my root directory).
I would like to make a mod_rewrite to this file so that one may go to mydomain.com/events
I tried simply doing something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule %events$ events.php [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which didn't work.
I also found I could add rewrite tags via wordpress using its inbuilt functions like so:
add_rewrite_rule('^events?','/events.php','top');
This also didn't work - I simply end up on my default 404 page as though its reached a non-existent page within my wordpress.
What am I missing here?
Correct me if I’m wrong, but if all you want is for someone who goes to example.com/events to be directed to example.com/events.php, than you can just do a redirect like so.
Redirect 301 /events http://www.example.com/events.php
Try the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^events /events.php [L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The directive that you had set for the URL would not match:
RewriteRule %events$ events.php [L]
Also, the directive is now below the condition that checks for an existing file, so that it never matches a direct request for events.php.

Resources