.htaccess: Redirect to requested uri - .htaccess

I have LiteSpeed web server and LScache running. For this I want to improve request times. LiteSpeed web server is Apache compatible and supports almost all Apache directive, so for my need it doesn't matter if it is LiteSpeed or Apache regarding redirect .htaccess rules. What I want to do is to define a redirection rule to the requested URI if a certain cookie doesn't exist.
RewriteCond %{HTTP_COOKIE} !ls_smartpush [NC]
RewriteRule ^(.*)$ the_requested_uri.com/? [R=302,L]
Is there any way to use the requested URI as a variable and to use this variable for redirection location?

you can capture it from your rewrite rule pattern
in your example rule ^(.*)$ will be captured as the requested URI
e.g https://example.com/something.html -> it will be something.html , and you can use it as $1 in your rule
you can also enable debug log on LSWS, it will log out how exactly it interpreted the rewritecond/rewriterule and what it redirected into , for debug

Related

How to Rewrite Rule for folder permission error?

I have assets[/admin/assets] directory which I restricted access from browser.
So whenever user requested on [/admin/assets] , I want to rewrite rule to [admin/index.html].
I tried with below setting , but this doesn't rewrite to expected path but show permission access error with 403.
RewriteRule /assets /index.html [L]
I have one solution for that by handling ErrorDocument. But I don't prefer it that way, I want to handle by RewriteRule .
This can be achieved using the mod_rewrite module in Apache web server. You can add the following code in your .htaccess file or server configuration file to rewrite the URL:
RewriteEngine on
RewriteCond %{REQUEST_URI} /assets
RewriteRule ^assets/(.*)$ /admin/index.html [L]
The RewriteEngine directive turns on the rewriting engine.
The RewriteCond directive specifies a condition that must be met before the RewriteRule is applied. In this case, the condition checks if the requested URL starts with "/assets".
The RewriteRule directive specifies the actual URL rewriting. The pattern "^assets/(.)$" matches any URL that starts with "/assets/", and captures the rest of the URL into a group (.). The URL is then rewritten to "/admin/index.html". The [L] flag specifies that this is the last rule to be applied, so no further rewriting should take place.
I set by Directory module with Require All Denied
You can't block access and rewrite the request. The <Directory> container will take priority. (But there's no point blocking the request when you want to rewrite it. You are essentially blocking it by rewriting.)
Remove the <Directory> block and directly inside the <VirtualHost> container (outside of any <Directory> section) use mod_rewrite:
RewriteEngine On
RewriteRule ^/admin/assets($|/) /admin/index.html [L]
Any requests to /admin/assets or /admin/assets/<anything> are internally rewritten to /admin/index.html.

Trouble with redirects with multiple directories

I have a domain with two versions and I need to redirect 1 of the versions
test.example.ca
test.example.ca/en
test.example.ca/fr
I need the first domain test.example.com to redirect to test.example.ca/en anytime someone hits it. but i don't want test.example.ca/fr to redirect to test.example.com/en/fr
this is what I've been trying with no success.
RewriteCond %{HTTP_HOST} =test.example.ca
RewriteCond %{HTTP_HOST} !=test.example.ca/fr
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
I understand the question such that you simply to not want requests to https://test.example.com/fr... to get redirected. So you want an exception.
I'd say this roughly is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{REQUEST_URI} !^/fr
RewriteRule ^ https://test.example.ca/en%{REQUEST_URI} [R=301,L]
Chances are that your question was wrong in a few details, to me it reads as if you were not really precise with your host names. But above should be the correct answer to what you actually asked.
You should implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"). That file should be located in the DOCUMENT_ROOT folder defined for your http host. And you need to make sure that the interpretation of such files is enabled at all (see the documentation for the AllowOverride directive for that).
It is a good idea to start out using a R=302 temporary redirection first. And to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.

Clicking on my web in google results redirects me back to google

Im helping a friend out on a website which is created using an online platform powered by plesk and theres an issue when trying to access this web through google.
Writing the domain directly in the browser works fine but when accessing it through a google search it redirects the user back to google.
What could be the issue?
this is my .htaccess file
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
#HTTP-HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://srad.wtf/es_ES/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
Ive removed the majority of the comments from the file to keep it clean
As stated in comments, there doesn't appear to be anything in your .htaccess file that would cause this redirect.
the redirect response ... appears to be coming from an Nginx server (possibly a front-end proxy), not Apache.
#MrWhite does that mean its something that I cant solve myself?
The Nginx server, from which the response is ultimately being served from/through (a front-end/caching proxy I suspect) is part of your server config - so you would expect to have some control over this - although "using an online platform" then maybe not?
However, the redirect(s) you are seeing may be coming from your application server/PHP (not Nginx or Apache). The problem isn't just with "Google Chrome" (as you have tagged) or even with Google SERPs. Any inbound link to the homepage is being 302 redirected back to itself (the HTTP Referer).
Not wanting to sound alarming, but this sort of redirect is quite typical of a site being hacked - as it is potentially damaging for SEO. Although since this only affects the homepage and is a 302 (temporary) redirect and you appear to have other language specific redirects in the application logic then this may just be a missconfiguration - although redirecting back to the "HTTP Referer" is quite a deliberate action!
For example, the following link to your homepage currently 302 redirects back to "this page"!
https://srad.wtf/
Workaround
Your site appears to be in two languages, as denoted by the first path segment, /en/ or /es_ES/ (default). The application logic appears to unconditionally redirect(302) to /es_ES/ if omitted (it is not deduced from the user's browser preferences or remembered for returning visitors).
You may be able to redirect to /es_ES/ early in .htaccess before the application kicks in. (By the same logic that requesting the HTTP homepage also works OK, since it is redirected to HTTPS early in .htaccess.)
Try the following, after the RewriteEngine directive:
RewriteRule ^$ https://example.com/es_ES/ [R=302,L]
Note that this is a "workaround", it doesn't fix the underlying problem.
Additionally...
#HTTP-HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://srad.wtf/es_ES/$1 [R=301,L,QSA]
This HTTP to HTTPS redirect is not strictly correct, as it unconditionally prefixes the request with /es_ES/ even when a valid language code might already be present. eg. Request http://example.com/es_ES/about (HTTP) and you are redirected to https://example.com/es_ES/es_ES/about (404). etc.
The HTTP to HTTPS redirect should simply redirect to the same URL-path (resolve any other language/path issues elsewhere*1). For example, this should be written:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com/$1 [R=301,L]
The QSA (Query String Append) flag is not required since the query string (if any) is passed through by default, unless you create a new query string on the substitution string (the QSA flag would then be required to append the query string from the original request).
(*1 To some extent, the preceding "workaround" resolves the missing language code.)
Strictly speaking, the language should be defaulted conditionally based on the value of the Accept-Language HTTP request header - but this is best done in PHP, not .htaccess.

Limit access to subdirectory via specific SERVER_PORT using htaccess

I've been trying to limit access to a specific subdomain via the server port, e.g. it can only be accessed from subdomain.domain.com:8443 and no other ports.
I'm currently using hostgator for my webhost, and it's already been setup such that subdomain.domain.com points to the correct subdirectory.
Now in the htaccess file, I'm currently trying this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /subdomain
RewriteCond %{SERVER_PORT} !^8443$
RewriteRule ^ - [F]
RewriteCond %{SERVER_PORT} ^8443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/subdomain/$1 [R=301,L]
As far as the blocking of other ports goes, it seems to work since accessing either subdomain.domain.com or www.domain.com/subdomain, I get a 403 forbidden page. But I can't get it to load the normal content correctly when I do access it via subdomain.domain.com:8443 or www.domain.com:8443/subdomain. Am I doing the rewrite conditions and rules correctly?
Thanks!
The R=301 tells the server to do a redirect, not a (silent) rewrite. So, when you load via 8443, the user is redirected to http://%{HTTP_HOST}/subdomain/$1 without the port 8443 specification and they are then blocked by the first rule. If you do a curl -I on subdomain.domain.com:8443 you should see the 301 redirect code rather than 200.
Remove the R=301 and remove the full domain specification in the final RewriteRule to leave:
RewriteRule ^(.*)$ subdomain/$1 [L]
This should do a silent rewrite of the content.

URL rewriting: non-redirect but changing url

I have configured my .htaccess file for url rewriting.
I'm just using the [L] flag, no [R] flag for a rule.
When I'm testing it in my browser, the page is called but... with the parameter url appearing. This is what I don't want, of course.
My .htaccess file:
RewriteEngine on
RewriteRule ^testing/([0-9a-zA-z_-]+)/([0-9a-zA-z_-]+)$ http://localhost/testing/index.php?a=$1&b=$2 [L]
Is it because I'm testing it on a localhost?
When you use a URL with a FQDN then it automatically redirects, simply because in order to internally rewrite the URI to http://localhost/... the handler at the end of the URL-file mapping processing pipeline redirects (with a 302) anyways. You either need to pick the local path for the target or if it's in a different place than the server/vhost that is processing the request, use the P flag:
Pointing to a local URI of the server/vhost:
RewriteRule ^testing/([0-9a-zA-z_-]+)/([0-9a-zA-z_-]+)$ /testing/index.php?a=$1&b=$2 [L]
Using the P flag to proxy the request:
RewriteRule ^testing/([0-9a-zA-z_-]+)/([0-9a-zA-z_-]+)$ http://localhost/testing/index.php?a=$1&b=$2 [L,P]

Resources