Bit of an odd question, but I'd like to have a query string set on all of my URLs. If the parameter isn't set (or is empty), then I'd like to redirect to include a default.
For example:
example.com would need to requrect to example.com?param=a
example.com?param would also need to redirect to example.com?param=a
If the param is set and is part of a list of known values, then it should carry on as normal:
example.com?param=(a|b|c|d) would go to the respective page a,b,c or d
Some pages of the site use other parameters to sort and paginate, so the rules cannot assume that this is the only query string.
I've tried a couple of things, but kept getting stuck in a redirect loop. This is trying to set the default param:
RewriteCond %{QUERY_STRING} !(^|&)param=(a|b|c|d)($|&)
RewriteRule ^(.*)$ /index.php?rq=$1¶m=a [L,QSA]
The main CMS rewrite rule is:
RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
RewriteRule ^(.*)$ /index.php?rq=$1 [L,QSA]
Any help would be great!
RewriteCond %{QUERY_STRING} !(^|&)param=(a|b|c|d)($|&)
RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
RewriteRule ^(.*)$ $1?%{QUERY_STRING}¶m=a [L]
RewriteCond %{REQUEST_URI} !^\/*(index\.php|blog|admin\/assets|site\/assets|robots.txt|sitemap(|\-[0-9]+)\.xml|products.xml|favicon\.ico)
RewriteRule ^(.*)$ /index.php?rq=$1 [L,QSA]
Related
Hey guys I'm having a bit of trouble getting my htaccess to redirect properly and was hoping for some help.
I'm expecting DEV-domain.com?CampID=AB12345 to redirect to
http://DEV-www.domain.com/landing/external-marketing/direct-mail/AB?CampId=AB12345
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=
RewriteRule (\w{2})(\w{5})$ http://DEV-www\.domain\.com/landing/external-marketing/direct-mail/$1?CampId=$1$2 [R=301,L]
Unfortunetly I can't get it working for some reason?
Because the RewriteRule matching is meant for the url path, not query strings. Try this:
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=(\w{2})(\w{5})
RewriteRule .* http://DEV-www.domain.com/landing/external-marketing/direct-mail/%1?CampId=%1%2 [R=301,L]
also you don't need to escape dots . in the target url, only in matching patterns. And be aware that if you decide to make your target url CampID instead of CampId, you need to put in another condition:
RewriteCond %{REQUEST_URI} !^/landing/external-marketing/direct-mail/
to avoid an infinite redirect as a target with CampID would match your RewriteCond rule...
I want to get rid of some query string on my whole website (explicitly facebook shared/like query which usually begin with fb_action).
I thought about using .htaccess to do that.
I want this:
Link 1)
http://example.com/documents/de_desmarais_en_sirois?fb_action_ids=10151430962018298&fb_action_types=og.likes&fb_source=timeline_og&action_object_map={%2210151430962018298%22%3A157643874359578}&action_type_map={%2210151430962018298%22%3A%22og.likes%22}&action_ref_map=[]
to look like:
Link 2)
http://example.com/documents/de_desmarais_en_sirois
In my .htaccess, I added:
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) /$1? [R=301,L]
But, when I go on the original link, I am directed to my root folder example.com/pages.php, but I want to keep the first part of the URL which is example.com/documents/de_desmarais_en_sirois. I don't want to go to my root.
What should I modify/do?
I assume you've added those rules to the htaccess file in the /documents/ directory?
You'll need to remove the leading slash in your rule's target and add a rewrite base:
RewriteBase /documents/
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) $1? [R=301,L]
The query string match could be improved a bit though:
RewriteCond %{QUERY_STRING} (.*)(^|&)fb_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
RewriteCond %{QUERY_STRING} (.*)(^|&)action_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
To remove only the facebook query string parameters, if you have other parameters that you want to preserve.
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]
This is my current .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/
RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]
Basically, it takes up to five "Friendly url folders" and assign the value to varibles and then, send those to my index.php page
IE: http://www.example.com/ford/focus/grey/
$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';
So far, so good.
Now, I need to integrate a 301 instruction (RegExp?) in that same .htaccess because initially, I had GET parameters like this :
IE: http://www.example.com/ford/focus/grey/?lang=fr
I need to get rid of all GET variables because Google sees it as duplicate content (even if I'm using the nofollow attribute on my languages links)
IE: http://i.want.to.keep/my/url/?AND_DUMP_GET_VARIABLES
http://www.example.com/ford/focus/grey/?lang=fr
http://www.example.com/ford/focus/grey/?lang=en
http://www.example.com/ford/focus/grey/?lang=sp
==> http://www.example.com/ford/focus/grey/
Logically, the instruction should be interpreted between the first and the second block but I just don't know where to start. Any hints?
THANKS!
As I understand you want to get rid of the QUERY STRING and redirect (301 Permanent Redirect) to the same URL but without QUERY STRING.
The rule below will redirect EVERY request that has query string:
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
1. The ? will do the magic -- will strip query string
2. You desperately need this line: RewriteCond %{ENV:REDIRECT_STATUS} ^$. The problem is that it may not work on your Apache setup and I cannot give you what exactly you need to make it work (it works fine on my vanilla Apache v2.2.17 on Windows).
After rewrite (internal redirect) occurred, it goes to next iteration and Apache starts matching all rules from the top again but for already rewritten URL. If we not add the above line, then mod_rewrite will apply the above rule to rewritten URL form and you will end up with all URLs get rewritten to /index.php with no parameters at all.
If the above will not work, then try the code below:
# do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
With help of # do not do anything for already existing files and folders rule, mod_rewrite will stop rewriting after URL will be rewritten to /index.php?p1=... format.
In case the above will not work at all (better -- in addition to the above -- I would suggest adding this anyway) use <link rel="canonical" href="FULL_PROPER_RUL"/> in your page:
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
http://www.google.com/support/webmasters/bin/answer.py?answer=139394
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]