Magento category urls infinite redirect - .htaccess

In Magento, I currently have on the site category urls which have no trailing slash, i.e. www.example.com/gifts
I want to change category urls to have a trailing slash, such as www.example.com/gifts/
To do this I went into the admin section, and edited the SEO section under system -> Configuration -> 'Catalog' -> Search engine optimizations.
There I added a '/' in the Category URL Suffix box.
That is all standard, and after rebuilding the index, the site does indeed display all category urls with a trailing slash, e.g. www.example.com/gifts/
The problem is, when I click on www.example.com/gifts/ (or any category now), I get an infinite redirect loop.
Chrome says "Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.", and doing a CURL -I -0 on the url shows a 301 redirect to Location: /gifts/.
I have checked my htaccess file, and there are definitely no rules related to trailing slash or category redirects.
EDIT: I have logging enabled on htaccess too, and I keep getting lines like:
strip per-dir prefix: /var/www/blah/app-core/index.php -> index.php
Is that normal?
Has anyone seen this issue before, or have any idea at all what might be causing this?
Its a complete blocker for me, so any insight or help would be very appreciated.
Thanks
Paul

Ha ha!!! Sorry, going a bit mad here!
Finally figured it out - I think it might be a bug in Magento 1.4.
In the Mage/Core/Model/Url/Rewrite.php file, request path is given as:
$requestPath = trim($request->getPathInfo(), '/');
So my request of '/gifts/' becomes 'gifts'. Magento then tries to append a slash, and it cycles over ad infinitum.
When you change the trim() to an ltrim(), it works. I.e.
$requestPath = ltrim($request->getPathInfo(), '/');
Now '/gifts/' is 'gifts/', and www.example.com/gifts/ is a 200!
Of course its a pain now to have to extend Magento just for such a minor tweak...

It sounds like your url rewrite table needs to be rebuilt. Login to your admin area and reindex the url rewrites.

Related

IIS URL Rewrite pattern from subdirectory to subdomain with custom path extraction

Previously there was this...
http://www.website.com/blog/post/2013/04/16/Animal-Kingdom's-Wild-Africa-Trek.aspx
(notice the apostrophe in 'Kingdom's')
and its now located at:
http://blog.website.com/post/Animal-Kingdoms-Wild-Africa-Trek
so, breaking it down the parts are...
remove .aspx from the end of the URL
map the call from www. to blog. and remove the blog part of the path
remove the date from the URL
remove the apostrophe
I understand how to redirect the subdirectory to a subdomain, but im stuck extracting the other parts of the path correctly, and cleaning out the apostrophe too.
A complete solution would be a great help, thanks in advance.
There is no way to remove all ', because it can be more than one. You could try following regexp (it allows up to 4 apostrophes), but it is quite "danger":
/blog/post/\d+/\d+/\d+/(([^']*)'*([^']*)'*([^']*)'*([^']*)'*).aspx
And redirect URL will be:
http://blog.website.com/post/{R:2}{R:3}{R:4}{R:5}
Bellow screenshot of my IIS Rule:

The trailing slash in home page

I will be really greatful if someone helps me with this.
Let's consider these 2 URLs (both returning 200 in the response header):
www.foo.com/something
www.foo.com/something/
Google considers these 2 URLs different despite both having the same content which leads to a duplicated content problem. To solve the issue it is advised to either use the 301 permanent redirect to redirect one URL to the other or use the rel="canonical" attribute. source
Wordpress blogs deal perfectly with this matter. When adding the trailing slash to my internal links, I was redirected to URLs without the trailing slash (301 response).
The problem is the redirect is only happening with internal pages. My homepage seem to return a 200 response with or without the trailing slash. Should I leave it as it is or force a redirect with the .htaccess file?
p.s.: The backlinks to my website have 2 different hrefs (with and without the trailing slash). Should I change those backlinks to a unique href or redirect one to the other?
Use this link to add trailing slash to end of your url
It doesn't matter whether your Backlinks are with slash or not, because after implementing techniques mentioned in above address, search engines will assume your pages only with slashes. Remember because of past indexing you should wait until former index to be deleted. or you may use 301 redirect to pages with slash. Basically this will take some time until search engines came again and find your redirect rules, too... .
By home page I assume you mean the page shown when you enter just the domain.
With or without the slash represents exactly the same URL. Nothing to worry about and nothing you can do.

Remove trailing ? [question mark] from url - empty query string

I am moving a website to a new Open Cart install and I have set up 301 redirects to keep any SEO value.
I had to use the following trick here - http://www.itsadam.co.uk/opencart-301-redirect-not-working-seo-fix-route/ - to work around an annoying issue with the way Open Cart handles url rewrites. So, I have this sort of thing in my .htaccess file currently:
redirect 301 /products-page /products?
However, this now leaves my rewritten urls with a trailing ? - an empty query string effectively:
http://www.mysite.com/products?
Is there a way I can use rewrites to match and ditch any superfluous question marks? (I need to retain any actual query strings).
I encountered the same issue in opencart. Rather than follow the link to your solution do it like this:
RewriteRule ^original-url$ new-url [L,R=301]

mod_rewrite so a specific directory is removed/hidden from the URL, but still displays the content

I'd like to create a rewrite in .htaccess for my site so that when a user asks for URL A, the content comes from URL B, but the user still sees the URL as being URL A.
So, for example, let's say I have content at mydomain.com/projects/project-example. I want users to be able to ask for mydomain.com/project-example, still see that URL in their address bar, but the browser should display the content from mydomain.com/projects/project-example.
I've looked through several .htaccess rewrite tips and FAQs, but unfortunately none of them seemed to present a solution for exactly what I've described above. Not everything on my domain will be coming from the /projects/ directory, so I'd imagine the rewrite should check to see if the page exists first so it's not appending /projects/ to every url. I'm really stumped.
If a rewrite is not exactly what I need, or if there is a simple solution for this problem, I'd love to hear it.
This tutorial should have everything that you need, including addressing exactly what you are asking: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html . It may just be a matter of terminology.
So, for example, let's say I have content at mydomain.com/projects/project-example. I want users to be able to ask for mydomain.com/project-example, still see that URL in their address bar, but the browser should display the content from mydomain.com/projects/project-example.
With something like:
RewriteRule ^project-example$ /projects/project-example [L]
When someone requests http://mydomain.com/project-example and the URI /project-example gets rewritten internally to /projects/project-example. Note that when this is in an .htaccess file, the URI /project-example gets the leading slash removed when matching.
If you have a directory of stuff, you can use regular expressions and back-references, for example you want any request for http://mydomain.com/stuff/ to map to /internal/stuff/:
RewriteRule ^stuff/(.*)$ /internal/stuff/$1 [L]
So requests for http://mydomain.com/stuff/file1.html, http://mydomain.com/stuff/image1.png, etc. get rewritten to /internal/stuff/file1.html, /internal/stuff/image1.png, etc.

htaccess redirects that ignore query string

I've recently inherited a webserver whose IP previously belonged to a well known band's forums.
Problem is I'm now drowning in 404 errors!
The URLs in question look like this:
http://[server_ip]/forum/ucp.php?mode=register&coppa=0
http://[server_ip]/forum/viewtopic.php?f=3&t=45330
http://[server_ip]/forum/index.php+++++++++++++++++++++++++++++++++++++++++++++++Result:+%ED%E5+%...
http://[server_ip]/forum/viewtopic.php?f=3&t=44597&start=0
In an ideal world I would like to redirect any traffic going to /forum/ucp.php, /forum/viewtopic.php or /forum/index.php elsewhere regardless of query string.
Or anything going to /forum/.* elsewhere, if that's doable.
I've tried a number of different solutions with little success, any help appreciated.
Assuming that you want to redirect all traffic to /forum/.* to http://mysite.com/somedirectory, which you can replace with the actual URL you wish to redirect to, you can add the following to the .htaccess in the root directory of your sites domain.
RewriteEngine On
RewriteBase /
#for any request starting with forum
RewriteRule ^forum/ http://mysite.com/somedirectory? [NC,L,R=302]
Be sure to keep the ? if you want to remove the query string params from the original query. If you want to make it a permanent redirect, change the 302 to a 301.
Don't really know what you have tried so far but this site will probably help you.
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

Resources