.htaccess redirect with variable - .htaccess

I already have a .htaccess file with the following lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.site\.com
RewriteRule .* http://site.com/%1 [R]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]
What it is doing is taking a page name in the form of page1.site.com and turning that into site.com/page1 in the URL which works. I want the URL to stay looking like page1.site.com while maintaining the current functionality.
Also it currently breaks when a user types in www.page1.site.com...when they type that it should take page1 as the variable and pass it through. Any ideas?

You'll want the PT directive for RewriteRule. Look for it at: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [PT]

Related

two urls one multilingual site with Joomla and .htaccess

I have built a multilingual site in Joomla! 3.1.x Dutch and English, using the multilingual functions native to Joomla! 3.1.x. I have two domain names that I want to go to this site, one to the Dutch side, the other to the English side.
http://www.internationalerozekerk.nl
http://www.internationallgbtchurch.org
Number 1 should go to: index.php?lang=nl
Number 2 should go to: index.php?lang=en
In the .htaccess I have added this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule (.*) ^internationalerozekerk.nl/index.php?lang=en$1 [L,R=301]
This redirects the English URL to internationalerozekerk.nl/index.php?lang=en. However, the address bar still reads: internationalerozekerk.nl/index.php?lang=en and not internationallgbtchurch.org
I haven't found anything to make the two URLs stay in the address bar.
Any suggestions?
Thanx,
Thom
You can use these rules instead for internal redirects:
RewriteCond %{HTTP_HOST} ^(www\.)?internationallgbtchurch\.org$ [NC]
RewriteRule ^$ /index.php?lang=en [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?internationalerozekerk\.nl$ [NC]
RewriteRule ^$ /index.php?lang=nl [L,QSA]
Reference: Apache mod_rewrite Introduction
Try removing the hostname and the R flag and remove the $1.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en [L,QSA]
For the other site:
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.nl [NC]
RewriteRule ^(.*)$ /index.php?lang=nl [L,QSA]
Adding the $1 at the end appends the request URI, which will completely mess up the query string. If you want the path to be sent as another param, then you need to be explicit about it (for example, "path"):
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en&path=$1 [L,QSA]

.htaccess, Rewriterule not working as i want

I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).

htaccess issue redirecting certain urls

I have a bunch of .htaccess rules that follow this format
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
Now I need to put in a new rule to include a category string in my URL and was given this code
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
But it's not being fired for some reason, can someone please show me how to change the above string to match the rule further up.. so I check for some condition before executing the rule?
I only need this rule to fire if the url is in the format http://www.lazygamer.net/post-name/ and not when the url is in the format http://www.lazygamer.net/category/post-name/
RewriteCond %{REQUEST_URI} !^/category/.*$
RewriteRule ^([^/]+)/ /category/$1/ [R]
My htaccess file now looks like this and solves the problem
RewriteCond %{HTTP_HOST} !^images.lazygamer\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/(wp-content|wp-admin|wp-includes|fixed|contact-details|advertise|about|category|submission|images|ps3|xbox-360|wii|other-news|video).*$
RewriteCond %{REQUEST_URI} !^\/$
RewriteRule ^(.*)$ http://www.lazygamer.net/fixed/$1 [R=301,L]
The second last line ignore the root folder which kept me up all night...

Htaccess Rewrite Cond or redirectmatch to remove query string from front of URL

I remember that redirectmatch can't handle question marks but how can I match this url:
http://www.mysite.com/es/?lang=es&url=index.php&option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etcetcetc`
to remove the lang=es&url= from before the index.php when there is a folder name present?
My problem would be solved if I could either remove the /es/ folder from the URL when presented with the ?lang=es&url= query string or I could remove the query string ?lang=es&url= from the URL when the folder is /es/
There are about 11 languages, with country codes fr, de, etc and one odd one out zh-CN. This is just past my capabilites at the moment. Thanks for taking the time to read this and any help would be greatly appreciated.
EDIT: mainly working now. I'm just having a small problem with the zh-CN language as it seems to be acting differently from the other en, fr, de etc, languages which are doing what I want, staying in English even when double clicking on another language. However, the zh-CN language redirects to the homepage with http://www.seed-city.com/?lang=zh-CN&url=index.php&zh-CN
I currently have this in my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/../
RewriteCond %{QUERY_STRING} lang=..&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(zh-CN|zh-TW)/ [NC]
RewriteCond %{QUERY_STRING} lang=([a-z]{2}|zh-CN|zh-TW)&url=index.php&(.*) [NC]
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
I have much more after but this is the relevant part. Thanks for your time. Natastna2.
If I understood your requirement properly this will work in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/es/
RewriteCond %{QUERY_STRING} lang=es&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
Using above role a URL like this: http://www.mysite.com/es/?lang=es&url=index.php&option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etc
will be redirected to:
http://www.mysite.com/es/index.php?option=com_virtuemart&page=shop.browse&category_id=42&product_type_id=1&product_type_1etc
EDIT
As per your edited section here are the rewrite rules that should work for now:
RewriteCond %{REQUEST_URI} ^/../
RewriteCond %{QUERY_STRING} lang=..&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(zh-CN|zh-TW)/
RewriteCond %{QUERY_STRING} lang=(zh-CN|zh-TW)&url=index.php&(.*)
RewriteRule ^(.*)$ /$1index.php?%2 [R=301,L]

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources