Pass variable from request_uri to destination url - .htaccess

I need to create an htaccess rule that pass variable, if exists, from request_uri to destination url.
So, if request url contains "test=something" it should adds it to the new url if it doesn't already contains "test" var.
Thanks in advance.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)test=[^&]+ [NC]
RewriteCond %{HTTP_REFERER} /index\.php\?(?:.+?&)?(test=[^&]+) [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [L,QSA,NC,R=302]
However do keep in mind that HTTP_REFERER can be spoofed by clients.

Related

how to redirect from www.example.com/Partnerlogin?AuthID=qwer to www.example.com/PartnerLogin.php?AuthID=qwer

We used to have a java application and in that url used to be http://www.example.com/PartnerLogin?AuthID=qwer but now we have moved to a PHP based application but the URL were given to various partner sites for linking back. The new url should be like http:www.example.com/PartnerLogin.php?AuthID=qwer
I am using following in .htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^authID=$ [NC]
RewriteRule ^PartnerLogin?authID=(a-z0-9+)$ PartnerLogin.php?authID=$1 [L]
please help.
You can use the following:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} authID=([a-z0-9]+) [NC]
RewriteRule ^PartnerLogin$ PartnerLogin.php?authID=%1 [L]
Query strings cannot be captured in RewriteRules, and so it needs to be capture in the condition only. Then, you use %1 in the new URI.

Redirect to different location based on presence/absence of a parameter

Is there any way to differentiate a Redirect on the basis of whether it has parameters or doesn't?
Example:
I want to redirect everything targetted at
http://www.example.com/news.php?id=123
to
http://www.example.com/old-news.php?id=123
but if no parameter is given, it should redirect to
news-overview.php, same path.
Cheers!
You'll need to use mod_rewrite and the %{QUERY_STRING} variable in order to do this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=
RewriteRule ^news\.php$ /old-news\.php [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^news\.php$ /news-overview\.php [L,R]
note that the query string automatically gets appended to the redirected URL. You can change the R to R=301 if you want to permanent redirect.

htaccess redirect root?var=abc to file.php?var=abc

I'm having trouble redirecting URLs like site.com/?var=abc to site.com/file.php?var=abc.
I tried a lot of redirect codes but none worked. I installed Wordpress in the root directory of the site and I'm trying to move the old content based on GET variables to file.php.
Thank you!
To redirect every empty url to file.php, here is what you need to write in your .htaccess:
RewriteRule ^/?$ file.php [R=301,QSA,L]
[QSA] tag will append your query string (?var=abc) to the new url.
To redirect the exact format /?var=abc, you'll need to use %{QUERY_STRING}:
# Check for query string, must be exactly "?var=abc"
RewriteCond %{QUERY_STRING} ^var=abc$
# Check for url, must be "/" or ""
RewriteRule ^/?$ file.php [R=301,QSA,L]
Solved by adding (.*)$ after the first variable (var), there were many other parameters in the link.
RewriteCond %{QUERY_STRING} ^var=(.*)$
RewriteRule ^/?$ /file.php$1 [R=301,QSA,L]

Redirect loop after .htaccess update

I want my WordPress based homepage to automatically start at abc.com/#about if the user enters abc.com.
I have added the following to my .htaccess file:
RewriteBase /
RewriteCond %{REQUEST_URI} !=about
RewriteRule ^$ /#about [L,R,NE]
The URL is changed, but the browser (chrome) give the error: “This webpage has a redirect loop”.
I get the same behavior if I use
RewriteCond %{REQUEST_URI} !=/#about
or
RewriteCond %{REQUEST_URI} !=#about
It could be because the URL contains a #, but I’m not sure.
Thanks in advance.
One solution is to use cookies. You can set a cookie after redirecting to URI with # and then delete it. Try:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} (^|\s)test=1($|\s)
RewriteRule $ - [L,CO=test:0:.your_site.com:-1]
RewriteRule $ /#about [L,R,NE,CO=test:1:.your_site.com]
Problem is that for the URL http://domain.com/#about browser doesn't send URI path after hash # to server and server will still see URL as http://domain.com/ which will make it redirect again and eventually browser shows looping error to you.
You can try this rule to stop the looping:
RewriteRule ^$ /index.html#about [L,NE,R]
OR else if index.php is your default file then:
RewriteRule ^$ /index.php#about [L,NE,R]
Try This:
RewriteBase /
RewriteCond %{REQUEST_URI} !=about
RewriteRule $ /%23about [L,R,NE]
The # part is replaced with URL encoded form %23. and the part which has to be rewritten should be $ , not ^$

.htaccess RewriteRule not working, need to generate a URL friendly

I have this dynamic link:
http://www.nortedigital.mx/article.php?id=36175&t=dobla_las_manos_el_snte__avala_reforma_educativa
and I need to convert in URL friendly like this:
http://www.nortedigital.mx/36174/se_enriquecio_elba_en_sexenios_del_pan.html
and i have this RewriteRule:
RewriteRule ^([^/]*)/([^/]*)\.html$ /article.php?id=$1&t=$2 [L]
but doesn't work. Please, anybody can help me?
You must capture the query string in a RewriteCond and use that in the RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
This redirects the client to request i.e. /36174/se_enriquecio_elba_en_sexenios_del_pan.html. Now you must server the real page. For that, we add an additional rule, similar to the one you already have in your question
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L]
But now, there's an endless redirect loop. We break this by using an environment variable. Here is the whole complete ruleset
RewriteEngine On
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L,E=SEO:1]
This rule does the redirect as above, as long as the environment variable is not set. And it serves the real page from article.php and sets the environment variable at the same time to prevent the loop.
You can use cookies for this purpose too. But that will break, if cookies are disabled in the client.

Resources