htaccess condition and rewrite - apache

i have this link
http://localhost/fukhtaccess
and i want redirect the link to http://server.localhost/index.php with GET params.
this is my htaccess, but don't work and not possibile test with some error for debug (thanks apache foundation!!!)
RewriteEngine On
RewriteCond %{HTT_HOST} ^localhost$
RewriteCond %{REQUEST_URI} fukhtaccess
RewriteRule (.*) http://server.localhost/index.php [QSA,L]

You have a typo: change HTT_HOST to HTTP_HOST.
Also, your second RewriteCond just complicates things unnecessarily. Do this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule fukhtaccess http://server.localhost/index.php [QSA,L]

better, this work fine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} fukhtaccess$ [NC]
RewriteRule (.*) http://server.localhost/index.php [QSA,L]

Related

redirect loop with mod_rewrite

I'm trying to add a language prefix to my pages through mod_rewrite
eg:
https://example.com/ > https://example.com/en/
and internally it need to translate to `https://example.com/?language=en
I've managed to do it for the base url, but when I try to do it with some other redirect implied, it's always end in an infinite loop.
eg:
https://example.com/product-p-22.html > https://example.com/en/product-p-22.html
and internally became `https://example.com/product_info.php?product_id=22&language=en
here is my actual config:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr)/
RewriteCond %{REQUEST_URI} .(php|html) [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ en/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)/$ index.php?language=$1&%{QUERY_STRING}
RewriteRule ^(.*)/(.*)-p-(.*).html$ product_info.php?products_id=$3&language=$1&%{QUERY_STRING}
I've tried many flags like L, END, DPI or some combination of those, but with no luck.
when I look at the debug logs, it seems to find the right url, but then restart parsing the default url:
pass through /var/www/example/product_info.php
init rewrite engine with requested uri /product-p-22.html
pass through /product-p-22.html
any though on what I'm doing wrong here?
Server version: Apache/2.4.41 (Ubuntu)
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^en [NC]
RewriteRule ^(.*)$ en/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php?language=en [NC,QSA,L]
RewriteRule ^([^/]*)/([^-]*)-p-([^.]*)\.html$ product_info.php?products_id=$3&language=$1& [NC,L,QSA]
Have it this way in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/+(en|fr)/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [NC,R=301,L]
RewriteRule ^([a-z]{2})/[^-/]+-p-([^./]+)\.html?$ product_info.php?language=$1&products_id=$2 [L,QSA]
RewriteRule ^([a-z]{2})/$ index.php?language=$1 [QSA,L]

.htaccess - Redirect domain/lang/ to subdomain for each language

I've been struggling with this for weeks and I can't get to the solution, I have to make a redirect in .htaccess of a multi-language website with all its pages following the same structure but with a subdomain for each language. In the following way that is valid for http as https:
domain.es/es/allpages to es.subdomain.com/allpages
domain.es/pt/allpages to pt.subdomain.com/allpages
domain.es/de/allpages to de.subdomain.com/allpages
domain.es/fr/allpages to fr.subdomain.com/allpages
domain.es/en/allpages to eu.subdomain.com/allpages
Any ideas?
I have tried the following but not sure if it is correct:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]
Thanks a lot!!!!
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,L]

Prevent redirect coming from external domains

I need to prevent that an external domain make a redirect to my domain. I tried using the following rules:
RewriteCond %{HTTP_REFERER} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work. I tried also:
RewriteCond %{HTTP_FORWARDED} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work too. any idea?
Why don't you simply precise the domain name without adding extra stuff?
Try this, it should work:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteRule (.*) /my_error_page.htm [R,L]
Tell me if it does
SOLVED: it works:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/my_error_page.htm
RewriteRule (.*) /my_error_page.htm [R,L]

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.

htaccess rewrite

I am working on a project and it utilizes a shortened url for redirection, I've gotten the following code to work perfectly:
XBitHack Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./getdeck.php?deck=%1 [L]
Now I want to add a second redirect rule for /a/([0-9A-Za-z]{4} but no matter how I write it, the new rule fails. Can anyone help explain what I'm missing?
Examples I have tried:
RewriteCond %{REQUEST_URI} ^/a/.*$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
or
RewriteCond %{REQUEST_URI} ^/a/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
You might have to repeat the first two conditions.