301 redirect loop in WordPress - wordpress

Initially used a 301 to redirect www.example.com/ → www.example.com/wp
Unfortunately, I didn't read all the 'don't use 301 unless your 100% sure it's permanent' and now I need to revert back to the original domain.
At first, I tried to do a regular site url/wordpress url change in Setting/General in the admin dash. Saved over the old .htaccess on the root that had the original 301 redirect. Didn't work.
I moved everything to the root directory because I was getting a 'This webpage is a redirect loop' error page. Cleared cache on all browsers. Still getting the redirect loop error page.
Checked my url redirection here: http://www.digitalcoding.com/tools/url-redirect-check.html
I have two prompts, the first is going through fine, the second is a big fat X in red:
301 Moved Permanently: www.example.com/ → www.example.com301 Another Redirect Detected: www.example.com
.htaccess looks like this:
#Use PHP 5.4
AddType application/x-httpd-php54 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php54/lib/php.ini
</IfModule>
ErrorDocument 401 default
# 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
Any insight?

Redirect 301 / http://www.example.com/
You get an endless loop because there is nothing in that code to tell it not to redirect www.example.com to itself.
You are correct that you cannot use the Redirect directive, and this is the reason; It is unconditional, and will cause a loop in the scenario you describe.
In order to prevent the loop, you must find a way to tell the code not to redirect www.example.com to itself. This can be done by using mod_rewrite, and specifically, the RewriteCond directive in mod_rewrite, to test the requested hostname and act accordingly:
Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# if requested hostname is non-blank
RewriteCond %{HTTP_HOST} .
# and if requested hostname is NOT "www.example.com"
RewriteCond %{HTTP_HOST} !^www\.example\.com
# redirect to same object in correct domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The first directive, Options, may or may not be required on your server. If it is not required, it may in fact not be allowed. Comment it out of you have trouble.
The second directive, RewriteEngine, is required once (and only once) at the top of your mod_rewrite code.
The third directive, the first RewriteCond, is only required if you do not use a name-based virtual (shared) server. It prevents an infinite loop if the client does not send a "Host" header with its request. Since it is impossible to access a name-based virtual server without a "Host" header, this line is not required on a name-based virtual server. No harm will come from leaving it in, except that it takes a little time to process it.
Note that this code will also redirect "example.com" to "www.example.com", and so serves to canonicalize your main domain name as well, preventing ranking dilution from having duplicate content on two variations of the domain.

Related

Infinite loop old index file old WP installation to New

The old site had a (302) redirect from / to /index.shtml.
The new site will be visible on / instead.
For SEO reasons we want a 301 redirect from /index.shtml to /.
However if I add that redirecting the htaccess file we get stuck in an infinite loop. So somewhere there still has to be a redirect from / to index.shtml, but where?
The old site was installed in the root directory /.
The new site is still in /dev/ (not my choice), however htaccess makes sure when someone goes to / they actually see the site in /dev/. The htaccess is saved in / and not /dev/.
Where could the old redirect from /index.shtml to / be?
My line of code in htaccess that creates a problem:
Redirect 301 /index.shtml http://www.example.com/
Could the error be caused by:
IfModule mod_rewrite.c (with brackets)
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule (with brackets)
Your Redirect is causing the infinite loop .as it redirects /index.shtml to your document root which already points to /index.shtml .
To fix this or to set the **/index.shtml** as your root directory handler ,you can use one of the following methods :
DirectoryIndex
DirectoryIndex index.shtml
DirectoryIndex will internally forword your **/** home directory to **/index.shtml** .
Mod-rewrite :
RewriteEngine ^$ /index.shtml [L]
This will rewrite your home directory to **/index.shtml** .
if you want to redirect /index.shtml to / , you can use the following rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.shtml\s
RewriteRule ^index.shtml$ / [L,R]
This will redirect /index.shtml to / without causing the infinite loop.
I know I'm coming late to the party, but the issue is the browser not the site. 301 and 302 redirects are both cached by recent browsers so you'll experience loops when you change them to point back to something that previously redirected.
You can test it with incognito mode and you'll see the redirect loop won't happen for you anymore. This is because incognito mode doesn't cache or observe the cache for redirects outside of its own session.
Unfortunately, there's no way to ensure that previous visitors don't get stuck in the loop which is why you should be very careful using 301 redirects. 302 redirects, as "temporary" redirects, will behave better with redirection loops, but the better solution would have been to use DirectoryIndex to load index.shtml as the default document instead of redirecting to it (which is what ultimately caused your problem).

Issue with htaccess 301 redirects on destination domain when using DNS pointing

I developed a new WordPress website at www-newdomain-com
(Sorry, using dashes instead of dots in the domain names as i cant post more than 2 links here without a reputation score of 10?..)
The old website at www-olddomain-com was prematurely redirected via DNS (#A records, www records) to www-newdomain-com. I therefore don't have access to a hosting container to create an htaccess file on www-olddomain-com to 301 redirect all the old pages to their new locations. I also have a parked domain setup for www-olddomain-com under www-newdomain-com - it's not an ideal situation, but i'm stuck :-)
The url page names are also completely different on both domains.
WordPress also stores the site address www-newdomain-com in the database and basically hard codes it everywhere.
Wordpress htaccess
# 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
Question:
How do I create 301 redirects in htaccess on newdomain in this above scenario?
If I use the following:
Redirect 301 /blog http://www.newdomain.com/articles
then it works initially, but as soon as I click any working link on www-newdomain-com thereafter, I receive a 500 internal server error.
If I use the following:
Redirect 301 /blog /articles
then it also works, but it displays the following in the address bar: olddomain-com/articles instead of www-newdomain-com/articles
I also have many of these: www-olddomain-com/blog/?cat=136
If I use:
Redirect 301 /blog/ /articles
then it loads the new domain 404 page and the address bar displays: http://olddomain-com/articles/?cat=136
I'm in the dark on this one and dont have any experience with htaccess other than the very basics. Any advice will be greatly appreciated, thanks!
First, you need to make sure you update the wordpress domain configuration to www-newdomain-com (go to /wp-admin/options-general.php to change it), otherwise it will rewrite every link to olddomain-com and mess up with your .htaccess rules.
This is why everything points back to olddomain-com for now.

FOSRestBundle double leading slash in route

I'm trying to handle a wrongly coded leading slash route in an Android application. It is trying to reach our system using:
//api/1.0/store/products/video/USD.json
but should normaly be
/api/1.0/store/products/video/USD.json
so this is causing a route not found exception in our app.
I cannot change the android application! I must find a way to make the wrong route work!
What i've tried:
Splitting the controller in two, one with the standard "/api" prefix and one with a prefix of "//api", didn't work, i think FOSRestBundle is fixing that for me live, so all my routes are still only 1 leading slash
Using a rewrite rule in .htaccess (See below) to rewrite the rule before i get problems, this would be the best strategy as it would keep my app and integration tests clean
Attempt using HTACCESS
RewriteRule /api/(.*) api/$1 [L]
This rule is supposed to work if i test it on "http://martinmelin.se/rewrite-rule-tester/" but in my htaccess it doesn't. Here's the content of my .htaccess.
Can anyone help me figure out a solution?
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite double leading slash routes to single leading slash routes
RewriteRule /api/(.*) api/$1 [L]
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
Okay, I tried, and failed to match the requested uri, so I will suggest you different approach, if you find it suitable. How about if we just replace all double leading slashes with single ones?
What I mean is something like that:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
The following tests on my project were successful:
/admin//orders => /admin/orders
//admin//orders//5 => /admin/orders/5
And lastly, I pasted yours as well:
//api/1.0/store/products/video/USD.json
gave this:
No route found for "GET /api/1.0/store/products/video/USD.json"
which is I believe what we are looking for. Hope you can use this solution as a temporary one until someone else provides a better one.

Installed Wordpress Network with www in the Site URL. How do I fix this?

I made a huge mistake. I set up a network without changing sitename to non-www, so now example.com (without www) is a non-existing page. How do I fix this? Changes in
settings
DNS
htaccess
?
I've tried htaccess redirect but wordpress sees the first request and still says www is missing.
We can't edit the Wordpress source code to redirect, as it will be broken on future updates. We can't forward all requests to the www-version of that request as that will break all subdomains.
I solved this with some edits to the root .htaccess file.
# This is probably how your file starts already
RewriteEngine On
RewriteBase /
# Then you add a condition: if the host starts with example.com
RewriteCond %{HTTP_HOST} ^example.com.*$
# And add a rule: redirect that url to the same url just with prepended with www
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Here the file continues with other stuff from WP
RewriteRule ^index\.php$ - [L]

301 Redirects from specific .html pages to new permalink structure failing

I've been searching online for over an hour trying to find out why my .htaccess file isn't working as expected, but all the docs and questions about .htaccess seem to say everything should be fine. i am using a wordpress site now and need to redirect some old links to *.html files to new permalinks (one by one as the naming convention has changed completely). so my .htaccess file looks like this:
# Redirects for old URLs
redirect 301 /contact_us http://rpcdev.thisisforge.com/contact/
redirect 301 /about_us http://rpcdev.thisisforge.com/about/
redirect 301 /clients http://rpcdev.thisisforge.com/work/
redirect 301 /clients/event.html http://rpcdev.thisisforge.com/work/event-retail-design/
redirect 301 /clients/identity.html http://rpcdev.thisisforge.com/work/identity-branding/
redirect 301 /clients/web.html http://rpcdev.thisisforge.com/work/web-design/
redirect 301 /clients/corp_lit.html http://rpcdev.thisisforge.com/work/brochures-publications/
# 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
The First three are working fine, it's the ones from specific .html pages that are not. I've tried putting the code both above and below the WOrdPress code, and I've tried switching the order of my 301s so the .html redirects are first, not last. but this doesnt help. Weirdly, sometimes the corp_lit.html redirect works ok, but the others don't. i can see no rhyme or reason to this!
Is my browser or OS (windows s7) caching the 301s from the .htaccess or something, back from when they were maybe incorrectly typed? i've tried emptying browser cache and flushing DNS in command prompt, but to no avail.
Pull
Any help much appreciated.
The Redirect directive links path nodes together, so your first /clients redirect is being applied instead of the following ones, try changing it to use RedirectMatch instead
RedirectMatch 301 ^/clients/?$ http://rpcdev.thisisforge.com/work/

Resources