i want to redirect all links with this format
https://besthost.tn/aze/viewticket.php?tid=VARIABLE1&c=VARIABLE2
to
https://besthost.tn/client-2/?ccce=viewticket&tid=VARIABLE1&c=VARIABLE2
the VARIABLE1 and VARIABLE2 must be inchanged while the redirection.
thank you
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /viewticket.php\?tid=([^&]+)&c=([^\s&]+) [NC]
RewriteRule ^ /client-2/?ccce=viewticket&tid=%1&c=%2 [NC,L,R]
This will work from the root and identify '/aze/viewticket.php' and then the tid=X&C=X pattern for redirection:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/aze/viewticket\.php [NC]
RewriteCond %{QUERY_STRING} ^tid=(.+)&c=(.+)
RewriteRule ^(.*)$ /client-2/?ccce=viewticket&tid=%1&c=%2 [L,R=301]
The R=301 part indicates a 301 'permanent' redirect which will be cached by the browser and is required if this is a permanent change in your website (for performance reasons).
Related
I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule
How to get rid of the error that causes continuous redirection? I would like these rules to work in both directions.
RewriteEngine On
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1
RewriteCond %{QUERY_STRING} ^name=(.*)$
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]
there's a redirection loop
you may use an environnement variable to check if your url is in a Redirection context
# redirect pretty url to real php
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=(.*)$
# force pretty url with 301 unless in a rewrite
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]
I need to redirect via .htaccess such an URL:
mydomain.com/?c=*
into
sub.mydomain.com/?c=*
For example:
must be redirected
mydomain.com/?c=123&a=1&b=2
into
sub.mydomain.com/?c=123&a=1&b=2
must not be redirected neither
mydomain.com/folder/?c=123&a=1&b=2
nor
mydomain.com/?cb=123&a=1&b=2
My Apache ver 2.4.29.
Thank you.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^c=.*$ [NC]
RewriteRule ^$ http://sub.domain.com/ [L,R]
I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.
Example:
I want this page:
http://sos-med.com/en/specific_page.html?print=1&ok=1
tp redirect (301) to the same URL with no Query string:
http://sos-med.com/en/specific_page.html
My code is:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?
I have no test server, so can you tell me if my code is ok?
The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?
I want only .htaccess solution, not PHP code.
You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
Try that.
Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –
Then you'd change what the rule matches against:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
Try this one instead :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]
I have subdomains: sub1.tld.com, sub2.tld.com and so on. This subdomains contain important info. But I want to prevent opening any other urls, for example: sub1.tld.com/anypage or sub2.tld.com/dir/more/anyurl.html. This urls must be redirected to main domain tld.com: tld.com/anypage or tld.com/dir/more/anyurl.html
So, i have to check 2 things before redirection:
it's any subdomain
it's not subdomain's root page
I created this rules:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://tld.com/$1 [R=301,L]
This set of rules works as expected (redirects sub1.tld.com/anypage to tld.com/anypage) but also redirects sub1.tld.com to tld.com. Where is the error? 2nd string is wrong?
Try this:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$ [NC]
RewriteRule ^/(.+)$ http://tld.com/$1 [R=301,L]
Or you can use this string to check if it root:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /\ HTTP/
For some unknown reason the most reliable for checking variable was %{THE_REQUEST}, not %{REQUEST_URI}.
%{THE_REQUEST} contains raw browser's query to web-server: for example, GET /anypage.html HTTP/1.1, if you are opening url http://anysite.com/anypage.html.
So, here you are complete working rule-set for .htaccess:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$
RewriteCond %{THE_REQUEST} ^get\ /(.+)\ http.* [NC]
RewriteRule ^(.+) http://tld.com/$1 [R=301,L]