I've got a URL with a query string that I am trying to redirect and I just can't get it to work. The original URL not only has a query string, it's a Joomla SEF URL and contains a ? within the URL as well. It's a mess. This is the original URL:
http://www.domain.com/menu-item/item/1234-article-title-here?.html&utm_source=XYZ&utm_medium=ABC&utm_campaign=the+campaign+name
I've tried several different rewrite conditions and rules and none I have tried are working. I am also putting the statements in .htaccess in the root since there are no actual sub directories. The query string does not need to transfer.
I tried this and it didn't work:
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here\?\.html$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
I also tried
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
And this
RewriteCond %{REQUEST_URI} ^1234-article-title-here\?\.html$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
and this
RewriteCond %{REQUEST_URI} ^1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
As well as the answer from #jon lin. I ended up finding a redirect that worked, posted below.
This is what finally ended up working:
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
Have you tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^\.html&utm_source=XYZ&utm_medium=ABC&utm_campaign=the+campaign+name$
RewriteRule ^menu-item/item/1234-article-title-here$ /page-you-want-to-redirect-to? [L,R=301]
The ? at the end of the target removes the query string so that it doesn't get included in the redirect.
Related
I am trying to rewrite a URL so that it is SEO friendly and excludes the product ID. Currently I am using this in my .htaccess file:
RewriteRule ^shop/product/([^/\.]+)/([^/\.]+)$ products.php?id=$2&name=$1 [NC,L]
For example rewrites it to:
shop/product/product-name/12
Is there a way that I can rewrite this URL so it removes or excludes the product ID from the end?
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
First set of Rules is considering that your product.php is present in root, in same path as .htaccess is present.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ /products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} \s/products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
OR Please use either above OR following rules at a time only in your .htaccess.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
I need to redirect following URL as follows:
https://www.example.com/news/amp/?s
to
https://www.example.com/amp?s
2nd URL has no "/" before ?s
I tried using following code, but failed.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^news/amp/?s
RewriteRule ^(.*)$ http://www.example.com/amp?s [R=301,L]
But its not working, can any one suggest correction for this code?
Try with below we are using %{THE_REQUEST} for matching instead of %{QUERY_STRING} which match only query string.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^/news/amp/?s
RewriteRule ^(.*)$ http://www.example.com/amp?s [R=301,L]
I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.
I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]
If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]
I need to rewrite a url so that it adds a url variable to all requests to the a sub-domain.
Example:
m.mydomain.com ==> m.mydomain.com?mobileFormat=true
This is the flag that triggers the mobile version of my site to load.
Should be easy right? Everything I have tried so far isn't working.
Here is the code I currently have in my .htaccess file:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ /index.cfm%{REQUEST_URI} [PT]
RewriteRule ^locale/([^/]*)$ /?siteLocale=$1 [L]
RewriteCond %{HTTP_HOST} ^m\.(.*)$
RewriteRule ^(.*)$ m.%1/$1?mobileFormat=true [QSA]
Thanks for the help.
I'm assuming that this is what you've tried:
RewriteCond %{HTTP_HOST} ^m\.(.*)$
RewriteRule ^(.*)$ m.%1/$1?mobileFormat=true [QSA]
QSA is Query String Append - if the URL they are trying to get to is:
m.mydomain.com?test=yes
then the above will rewrite this to:
m.mydomain.com?mobileFormat=true&test=yes
The solution I was looking for was:
RewriteCond %{HTTP_HOST} ^m\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !\.(xml|jpg|gif|png|js|css)$
RewriteRule .? $1?mobileFormat=true [QSA,L]
Thanks for all of the help.