Two parametrs in mod_rewrite rule - mod-rewrite

I have problem with mod_rewrite rule.
I'm trying to get adress:
www.site.com/en/page.html
from url
index.php?page=page&lang=en
I've tried a couple of hours and I can not get ... Unfortunately, I do not know at all mod_rewrite ...
Can someone a guiding me to a solution?
-
Peter

First, make sure mod_rewrite is enabled in your Apache configuration file.
Also, make sure htaccess files are allowed (AllowOverride All for your document root folder).
Then, put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteRule ^([a-z]{2})/([^/]+)\.html$ /index.php?page=$2&lang=$1 [L]
This rule allows you to reach any language/page, for instance:
http://domain.com/en/page.html --> /index.php?page=page&lang=en
http://domain.com/fr/something.html --> /index.php?page=something&lang=fr
If you want to restrict lang parameter:
For english only
RewriteEngine On
RewriteRule ^en/([^/]+)\.html$ /index.php?page=$1&lang=en [L]
For english and french (as an example)
RewriteEngine On
RewriteRule ^(en|fr)/([^/]+)\.html$ /index.php?page=$2&lang=$1 [L]

Thank you for your help - exactly is what I meant.
Roughly already beginning to understand what's going on...
From the documentation at hand I can handle it :)
-
Peter

Related

How to setup a 301 redirect using mod_rewrite

I must admit, mod_rewrites are still a bit of black magic for me and I try to stay well away from them unless I really, really need to us them, now I do!
I've just migrated my site to become a multi lingual site and in doing so I've made the following change to the structure.
oldwebsite.net/somefile.php >> newwebsite.com/xx/somefile.php
oldwebsite.net/somefolder/somefile.php >> newwebsite.com/xx/somefolder/somefile.php
Now, I know how to do individual 301 redirects, but I got tons of pages and the new structure hasn't changed the page name so I would like to avoid setting up hundreds of individual 301 redirects, help!!
Thanks,
Mikael
You can put this code in your oldwebsite htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} oldwebsite\.net$ [NC]
RewriteRule ^(.*)$ http://newwebsite.com/xx/$1 [R=301,L]

Forwarding only the root of a subdomain to a folder in the main domain (mod_rewrite)

I think I'm misunderstanding some fundamental thing here, as this is my first time using mod_rewrite.
I would like the following:
blog.example.com
blog.example.com/
blog.example.com/index.php
to redirect to:
example.com/blog
However, I would like all other cases to do nothing, e.g.
blog.example.com/foobar
blog.example.com/wp-admin
blog.example.com/wp-admin.php
This is my current .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} =blog.example.com
RewriteCond %{REQUEST_URI} ^|/|index\.php$
RewriteRule ^(.*)$ http://www\.example\.com/blog [QSA,L]
I have tried many variants but each time either all requests redirect, or none redirect, so mod_rewrite is definitely doing something, just not what I want.
I have skimmed through this post for anything relevant, but I think the issue is more that I'm missing some subtle fact due to inexperience. Would someone kindly point out my error? I know we don't need yet another mod_rewrite question on SO but I'm really struggling with this one. Thanks in advance.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} =blog.example.com [NC]
RewriteRule ^/?(index\.php)?$ http://www.example.com/blog [QSA,R,L]
Some basic points:
The second part of the rewrite rule is not a regular expression, so periods do not need to be escaped there.
I was not familiar with the =String option for RewriteCond. Thank you for teaching me something today (I was worried when I did not see a regex there but that should be fine)! I would add [NC] here since it will help match both lowercase and uppercase versions of your domain.
None of this will work unless your virtual host includes the blog.example.com subdomain as a proper alias, and DNS is set up for that subdomain.

ModRewrite .htaccess / SEO URLs syntax

I am trying to re-use some .htaccess code on a new site/server and it doesn't work correctly. I'm not an expert with URL rewriting so would appreciate it if anyone can see if my syntax is incorrect or if there is something else I need to check server side.
I am using the following code:
Options +FollowSymLinks
RewriteEngine on
# News Pages
RewriteRule ^news/$ /news.php
RewriteRule ^news/(.*?)/$ /news.php?article=$1
It works for the 1st level, /news/ but /news/article-1/ just loads the /news/ (news.php) overview page. /news.php?article=article-1 works correctly.
Server is running Apache 2.2.9 and PHP is in CGI mode.
Try it such way:
RewriteRule ^news/(.*?)/?$ /news.php?article=$1 [L]
RewriteRule ^news/?$ /news.php [L]
[L] in the end of instruction stops server cheacking of other instructions, if current one match.
So you write more specific ahead of more general.
If your rules work correctly being the only one in the .htaccess, then such organisation of them can help.
/? - this means that / at the end can absent or present

mod_rewrite understanding sub-directories whilst passing $_GET value server issue?

This may well be a server config issue; or simply a blindly obvious reason I'm missing...
Pre mod_rewrite URL:
www.example.com/subfolder/index.php?userName=x
The devised mod_rewrite:
RewriteEngine on
RewriteRule ^subfolder/[^/]([\w]*)$ /subfolder/index.php?userName=$1 [L]
It is my understanding that the above should allow navigation to: www.example.com/subfolder/x. However this causes a 404 error.
Rewrites without the sub-folder work fine; it is only when adding the subfoler to the mix things fall to put.
Your advice is much appreciated.
Try this one instead (works OK for me):
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder/index\.php$
RewriteRule ^subfolder/([^/]+)$ /subfolder/index.php?userName=$1 [L]
NOTE:
This rule is to be placed in .htaccess. If placed in server config / virtual host context, some small tweaking will be required.

URL rewriting problem

In order to convert dynamic URLs on my site www.kitesmovie.co.in to static urls. Eg: www.kitesmovie.co.in/stories.php?id=10 to www.kitesmovie.co.in/Barbara_Mori_Hrithik_Roshan_New_Movie.
I tried using rewriting rules in my htaccess files, but it did not work. Please tell me how to do this.
Thanks a lot in advance.
It's not really clear what you are trying to achieve.
When you say "convert dynamic urls to static urls", do you really want to have user to type in the ...?id=10 and the file named Barbara_Mori_Hrithik_Roshan_New_Movie lives on your server? I think, it's the opposite way - you want to let the user type in the long & nice title and actually resolve it to ?id=10
If this is the case, the #Mike's answer is nearly correct, you only have to swap the parts of the last line:
RewriteRule Barbara_Mori_Hrithik_Roshan_New_Movie stories.php?id=10 [R,L]
Another question - are you sure you have the .htaccess working? An easy way to check is to set the contents of .htaccess to
order deny, allow
deny from all
And see if you can still access that directory. If you can, this means the "deny from all" does not work. Then check your apache config - is .htaccess allowed in the particular virtualhost and/or directory.
Lots of missing info there. If you are using apache with mod_rewite installed, place this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule stories.php?id=10 Barbara_Mori_Hrithik_Roshan_New_Movie [R,L]
</IfModule>
You might want to have a look at this article: http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
Generally, you would use a RewriteRule something like this:
RewriteRule ^stories.php?id=10$ Barbara_Mori_Hrithik_Roshan_New_Movie [R=301,L]
If that's not working for you, then feel free to add more details to your question.

Resources