I need to add style=2 to all urls within a directory ONLY if the url does NOT already contain style=2.
Within the directory root, I have an .htaccess with the following:
RewriteCond %{QUERY_STRING} !(?style=|&style=)
RewriteRule ^(.*)$ %1&style=2? [R=301,QSA]
I know it's wrong. Can anyone help?
Change your code to this to make it work:
RewriteCond %{QUERY_STRING} !(^|&)style=2(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?style=2 [R=301,L,QSA]
Try this :
RewriteCond %{QUERY_STRING} !^(.*&)?style=
RewriteRule ^(.*)$ /$1?style=2 [L,QSA,R=301]
Related
I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?
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'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.
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.
ive this rules at htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
issue happend when redirect warcraft its redirect to
http://www.site.com/home/site/public_html/www.site.com/forumdisplay.php?f=480
any tip ?
I suppose you want to redirect it to http://www.site.com/forumdisplay.php?f=480
If so put it in your .htaccess file:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ http://www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
You must provide the full URL including protocol in your rule, in this case http://www.site.com/.....