URL Rewrite multilanguage site - php

I have a multilanguage website made using wordpress running on apache 2.2. The url structure is www.domain.it for italian and www.domain.it/?lang=es for (for example) spanish.
My client owns also www.domain.es. My goal is to have users write www.domain.es and be redirected to www.domain.it/?lang=es
this is my htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.it/$1/?lang=es [QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
the last four rows are for wordpress to provide index.php in some specific cases. The other two are mine to achive the goal. What I get now is a 302 page telling me that the document has moved (it says again to www.domain.es. If I add R=301 I get no more luck.
What am I doing wrong? Can anyone help me to stop banging my head around?

Do you want it to redirect? Or just handle the rewriting internally somehow?
You may need the L flag to tell it to stop trying to do anything else and just do that redirect:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.it/$1/?lang=es [L,QSA,R=301]

Related

.htaccess; Too many redirects if two different redirects in .htaccess

I've two types of URLs:
http://www.example.com/?content=contact_form.php
and
http://www.example.com/?content=product.php&id=20
I changed my whole URL system like this:
http://www.example.com/file/contact_form
and
http://www.example.com/product/I-m-the-title/20
Of course I made 301 redirect with .htaccess to tell Google and co. the new URL.
I made it like this:
# Rewrite URLs
RewriteEngine On
RewriteRule ^(ignore)($|/) - [L]
RewriteRule ^file/([^/]*)$ /?content=$1.php [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?content=$1.php&title=$2&id=$3 [L]
# Redirect old URL to new URL
RewriteCond %{QUERY_STRING} ^content=contact\_form\.php$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/file/contact_form? [R=301,L]
RewriteCond %{QUERY_STRING} ^content=product\.php&class=I-m-the-title$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/I-m-the-title/Test/20? [R=301,L]
My problem:
It's perfectly working for: http://www.example.com/?content=product.php&id=20
But for http://www.example.com/?content=contact_form.php I'm getting the message that it couldn't get opened because of too much redirect.
Does anybody know what I'm doing wrong? I hope anybody can help me soon because I have to fix it before Google misinterprets it.
Your rule cause an infinite loop because it is rewriting your uri to the same location again and again overriding your internal and external redirects.. To fix the Rewrite loop, add the following at the top of your htaccess
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Mobile site Rewrite Rules not working correctly for converted version of site if page call is in sub directory

My problem is a little difficult to explain but I think it has to do with the rewrite rules. Basically I have a script that converts a standard site to a mobile site the code is not really supported, by its dev on google code, so I figure I might get some help here I re-coded it a bit so it works with today's phones.
It works fine as long as none of the pages it must access are in a sub
so for example the site is www.mysite.com the script is in a sub-domain
m.mysite.com the script gets the pages from the main site but appear to be served from the mobile site, a example urls m.mysite.com/mypage.php
will work fine and any links on that page do work.
But if the page is in a dir like m.mysite.com/blog/myblog.php it will access that page and you will see /blog in our url but all the links on the page you click will show 404 because they all link as m.mysite.com/myblog.php not m.mysite.com/blog/myblog.php so it does not seem to know that for this page its in /blog even though it is clearly in the url why is it not in the links on the page?
Any ideas what might fix this would this be a rule issue? the htaccess is as follows.
DirectoryIndex phpmobilizer.php
Options +FollowSymlinks
RewriteEngine On
#RewriteCond %{REQUEST_METHOD} =POST
#RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ phpmobilizer.php?url=$1 [QSA]
The other issue I have is if it calls a page with a form it will not post I get 404 if I use the rules I have commented out, if I don't add them it will just reload the page no post. Maybe both problems are somehow related, my hope is some expert can help me sort this all out.
Tried to keep this as short as I could but a tad tricky to explain.
I am not very certain if it works for you. It works in my case with my wordpress page. Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?yoururl.com$
RewriteRule ^(/)?$ yoursubdirectory [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/kml/(.*)$ [OR]
RewriteRule ^.*$ - [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>
Cheers
Eric

Mobile Redirect with pretty URLs in .htaccess

I am trying to redirect my main site (www.mainsite.com) to my mobile site (m.mainsite.com) based on user agent in .htacess. I used the following code and it redirects to the m.mainsite.com properly.
RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\ CE|Windows\ Mobile|Windows\ Phone|nokia|UP.Link|UP.Browser)
RewriteRule (.*) http://m.mainsite.com/$1
However, when taken to the mobile site I seem to be losing the rewritten URL. So what should be m.mainsite.com/page1 goes to something like m.mainsite.com/index.php?url=page1.
I tried rewriting the URL using .htaccess on the mobile site but could not figure out how to remove the part after index.php. Can anyone help me figure out how to remove "index.php?url=" from m.mainsite.com/index.php?url=page1?
Or would it be easier to figure out how to address this via htaccess in the non-mobile version during the redirect.
Full code looks like:
[code]
RewriteCond %{HTTP_HOST} ^mainsite.com
RewriteRule (.*) http://www.mainsite.com/$1 [R=301,L]
RewriteRule ^(.*)\_(\?.*)?$ $1$2 [R=301,L]
RewriteRule .* - [env=REWRITE_ON:1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|gif)$
RewriteRule ^(.*)$ index.php?url=$1 [L]
RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\ CE|Windows\ Mobile|Windows\ Phone|nokia|UP.Link|UP.Browser)
RewriteRule (.*) http://m.mainsite.com/$1[code]
Thanks
Gary
Looks like you have other rules that's interferring. You need to put all of your redirect rules before any internal routing rules. The index.php?url=page1 looks like your routing rule.
Place the mobile redirect near the top of your htaccess file.

Magento multi language rewrites SEO fix?

I have made a website in with Magento in one language 2 years ago and now I would like to add another language.
My actual structure is like that
example.com/category/
example.com/about.html
example.com/product.html
and I would like to obtain a structure like this:
example.com/lang1/category/
example.com/lang1/about.html
example.com/lang1/product.html
example.com/lang2/category/
example.com/lang2/about.html
example.com/lang2/product.html
This is not a big deal since Magento allowes me us to do that simply logging in the backend ->System -> Configuration -> Web -> Add store code to Urls (YES)
I can't do this setting right now because before that I need to fix and do rewrites from the first structure to the new one.
example.com/everything_without_the_/lang1/_path to a permanent
example.com/lang1/everything
I need for example that the urls from the backlinks from posts from the socials and other webistes won't get a 404 error but will automatically be redirected with a 301 redirection to the equivalent page in the new structure.
So I would like to add a script like this in the natural language:
rewrite permanently all the urls which ends not with /lang1/ or /lang2/ to urls with the prefix /lang1/
I know that I could manually add rewrite rules with this schema in the Magento backend but I would prefer to know if it is possible to batch this directly from the database or with some script in the .htaccess or index.php and above all which one between these solutions will have a less negative impact in SEO and SERP.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee.
I hope this will help
Cheers
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/lang1/
RewriteCond %{REQUEST_URI} !(.*)/lang2/
RewriteRule ^(.*)$ http://www.domain.com/$1/lang1/ [R=301,L]
you can use .htaccess 301 url to make if possible with your own regex function
As you need some specific requirement to do with your language part
here i am giving you category example
Redirecting Subcategory URL with New Parent URL
Examples:
category/sub1 redirect to cat/sub1
category/sub2 redirect to cat/sub2
category/sub3 redirect to cat/sub3
This may be needed if you changed a parent Category URL or even if you want to remove the parent URL completely:
RewriteRule ^category/(.*) http://www.yourwebsite.com/cat/$1 [R=301, L]
OR to remove '/category/' completely from yourwebsite.com/category/sub and end up with yourwebsite.com/sub ('sub' can be any url):
RewriteRule ^category/(.*) http://www.yourwebsite.com/$1/ [R=301, L]
And also you can refer
http://www.learnmagento.org/magento-tutorials/301-redirects-in-magento/
http://blog.maximusbusiness.com/2012/10/magento-url-rewriting-regex-and-301-redirects-tips/
hope this will sure help you.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee. I hope this will help Cheers

htaccess redirect on mvc framework

I am working on the htaccess file for my mvc site. The software that the company purchased for the site works only without the www, so I was able to fix up the htaccess to allow www in the URL since most of our affiliates are going to try to use it anyway. However, this renders the siteurl.org/index.php/admin and the siteurl.org/index.php/members unreachable. I'm trying to exclude these URL's from the www forward to non-www but everything I know and can find seems to relate to non-MVC sites, and it seems that mvc sites are set up differently across the board so the examples I'm finding aren't working for me.
Here's my current htaccess (I had to comment out the forwarding lines to allow admins to access the admin section and affiliates to access the member section)
<Files ~ "serial.txt$">
Order allow,deny
Deny from all
</Files>
RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
If it could be addressed at the same time, I'd also love a pointer on how to clean up that URL so that we could type in siteurl.org/admin as opposed to siteurl.org/index.php/admin (same for members), and also to show the affiliate name in the URL (it's currently cleaning up the URL to remove the /aff/affiliateusername but affiliates would like to see their name in the URL). If anybody has a great link to specific resources on writing htaccess for MVC I would be eternally grateful. Thank you in advance for any assistance.
Let's be clear on three things before going into explanations:
Apache doesn't have a care in the world whether your site is built on the MVC approach/design pattern or not. It. Doesn't. Even. See. It. To it, it sees htaccess mod_rewrite rules.
Whoever puts a serial code in serial.txt on the root is just begging for it to get nicked using a file include vulnerability in PHP.
This is suspiciously similar to CodeIgniter in rewrite rules.
Now. Your rules:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This rule will match only if the http host does not start with www. . If this is true, then it'll redirect to the http ://www. version.
Based on your description, you want the opposite: Your CMS does not work with www.. So, you will want this:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://mydomain/$1 [R=302, L]
Note that you'll need to hardcode your domain in there. Sucks.
Next set of rules:
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
This is bog-standard - redirects aff/whatever to /index.php/aff/?aff=$1
For the future, change it to this:
RewriteRule ^aff/(.*)$ /index.php?/aff/?aff=$1 [L]
This will clean up the URL and prevent an Apache rewrite cycle.
Next one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
These will wildcard-match anything that does not exist. Same thing as before, change the last line to this:
RewriteRule ^(.*)$ /index.php?$1 [L]
This will, again, make the rewrite transparent.
P.S: get a real CMS developer. 301s are hardly useful.

Categories