Htaccess multilanguage mod_rewrite - apache

Ok, I've wasted a lot of time trying to figure this out but the more I'm trying to learn about rewriting rules, the less I seem to understand what's going on. I need to fix some rewriting rules for a codeigniter based project.
The application is a multilanguage CMS that basically works with modules, pages and posts and in a .htaccess file I need to check if the url is a page or a post and redirect to a appropriate controller.
So far in my .htaccess file i have the following:
#if I'm on the homepage and i have a language id
RewriteCond $1 ^([a-z]{2})/?$
RewriteRule ^(.*)$ index.php?homepage/index/$1 [PT,L]
#if module - this works ok when there is no language id
RewriteCond $1 ^(gallery|post|products)
RewriteRule ^(.*)$ index.php?$1 [PT,L]
# Rewrite all other URLs to index.php/URL - page controller
RewriteRule ^(.*)$ index.php?page/show/$0 [PT,L]
With the code above, if I enter
http://mydomain.com/gallery/something/9
everything is fine and dandy, dut if I add a language id like so:
http://mydomain.com/en/gallery/something/9
it doesn't work anymore, and I get a page not found/404 error from the page controller (redirect at the end of .htaccess).
So let's say that i have urls that can look something like this:
http://mydomain.com/gallery/something/9
http://mydomain.com/en/gallery/something/25
http://mydomain.com/fr/post/something/31
I need to somehow redirect those urls to
http://mydomain.com/gallery/something/9
http://mydomain.com/en/gallery/somethingelse/25
http://mydomain.com/fr/post/somethingelse/31
Notice that sometimes I have language id, and sometimes I don't.
Is this something that I should be doing by using htaccess? Is Codeigniter routing a better alternative?
Thank you for any help Stack Overflow :)

Try that:
RewriteCond $1 ^[^/]*/?(gallery|post|products)

Related

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

301 redirect query string to SEO friendly URLs through .htaccess

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

Rewriting old WordPress urls to new site's URL structure via .htaccess

Hey folks, I've migrated a site from WordPress to a new CMS, and I want to preserve the old URLs via a redirect.
The WordPress permalink structure was like so:
/2011/04/01/name-of-post
I've preserved the post slugs, so all I need to do is get rid of the date-based paths and redirect to my new directory structure:
/articles/view/name-of-post
My attempts thus far have looked like this (in my .htaccess file):
RewriteCond %{THE_REQUEST} /[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+) [NC]
RewriteRule ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+)$ /articles/view/$1 [L]
No luck yet. I tried %{PATH_INFO} in there as well, no dice.
Any help from those more versed in Apache rewrite rules than would be much appreciated.
Something like this should do.
RedirectMatch permanent ^/20../../../(.*)$ /articles/view/$1
It can be handled in one simple RewriteRule like this:
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+)$ /articles/view/$1 [L]

Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests

So I just inherited a site. The first thing I want to do is build a nice little standard, easy-peezy, CMS that allows for creating a page with any URL (for example: whatever.html).
Therefore, if user hits example.com/whatever.html, it should get any db info for whatever.html and display it. This is run of the mill stuff.
My problem is that there are quite a few pages on the site (all listed in the .htaccess) that need to continue to be accessible. For instance, /Promotions is linked to promotions.php via .htaccess, and I need it to stay that way.
Anyone know how I can construct the .htaccess file to allow specific rewrites to still work but to reroute all other requests through index.php?
Currently, I just have .htaccess show a custom 404 page which in turn checks the db for the url and displays it if it exists. This is an easy solution, but I know that some people have weird browser toolbars (dumb or not, they exist :) ) that autoredirect 404s, and I'd hate to annoy my users with these toolbars by not allowing access to certain pages.
Thanks so much for your help!
The RewriteRule for promotions should still work as it's not 404ing.
If the 404 handler is showing the page because it exists in the database then it should really be returning a 200 OK status (overriding the 404 one), so you should not get any issues with browser toolbars.
As for doing the rerouting you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(promotions|anotherone|somethingelse)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?p=$1
Here is another variant:
RewriteEngine on
RewriteRule ^/i/(.*)$ - [L]
RewriteRule ^/css/(.*)$ - [L]
RewriteRule ^index.php$ - [L]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

How to modify .htaccess for web app (in combination with WordPress 3.0.1)

My question is about .htaccess
I am using WordPress 3.0.1 as the front end of my web application.
I want most HTTP accesses to follow the normal WordPress flow.
However, I have created a special "Page" in WordPress that has embedded PHP code that powers my web app.
URL's of this form "http://site.com/app/" already go to the correct page.
URL's of this form "http://site.com/app/?a=alpha&b=beta" go to the same page, and pass parameters to my web app. This is correct, but the URL looks ugly.
Here is my question:
I want nice looking URLs of this format:
http://site.com/app/alpha/beta
to be rewritten this way ->
http://site.com/app/?a=alpha&b=beta
I have tried adding various things in .htaccess, but I can't get it to work. I typically end up with a WordPress 404 page being displayed.
Here is the .htaccess file that was created when installing WordPress 3.0.1
# .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
I am sure there must be lots of folks using WordPress as a front end for their web app, and others with expertise in .htaccess syntax. Any answers or pointers to solutions would be appreciated.
thanks in advance,
David Jones
dxjones#gmail.com
http://dxjones.com
David, don't know the answer for sure, but here's a thought.
You can use plugins like this: http://wordpress.org/extend/plugins/exec-php/
It let's you execute code from within pages or posts.
If you look into the WP documentation, you could easily tie another PHP app into WP.
Firstly, to use rewrite rules in .htaccess, two things are required:
Options +FollowSymlinks
RewriteEngine on
Then this rule will do what you describe:
RewriteRule ^app/([^/]+)/([^/]+) /app/?a=$1&b=$2 [L]
FYI ... [^/] matches any character except for "/"
and the [L] at the end will make this rewrite invisible to the user --- it's only passed to your application.
UPDATE
To make visitors using old links automatically redirect to the nicer URL (and possibly improve search engine mapping of webapp pages), add an additional rule --- like this one:
RewriteCond %{QUERY_STRING} a=([^&]+)&b=([^&]+)
RewriteRule ^app/?$ /app/%1/%2 [R=301]
The rule above turns the a and b parameters into your nice URL -- however, it would not pass on additional query parameters. (i.e. site.com/app?a=me&b=you&tag=zebra would redirect, but 'tag' parameter would be discarded in the process.) Here is a variation that allows for additional parameters:
RewriteCond %{QUERY_STRING} (^|.*&)a=([^&]+)&b=([^&]+)(&.*|$)
RewriteRule ^app/?$ /app/%2/%3?%1%4 [R=302]