Redirect using mod_rewrite in file .conf is not working - apache

In my server apache, I need to redirect URLs in a .conf file. In my file http.conf, located in /etc/httpd/conf, one of the lines contains the following configuration:
Include conf.d/*.conf
In the folder the setting is indicating, in /etc/httpd/conf.d, I have a file called redirect.conf that has the following redirect code inside:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?abracar/?$ /abracar? [L,R=301]
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?presente/decoracao/teu-sorriso/?$ /presente/decoracao/teu-sorriso? [L,R=301]
RewriteCond %{QUERY_STRING} ^p=2&PageSpeed=noscript$ [NC]
RewriteRule ^/?acessorios/confeitaria.html/?$ /acessorios/confeitaria.html? [L,R=301]
</IfModule>
This code should redirect the URL https://example.com/abracar?PageSpeed=noscript to https://example.com/abracar, but it is not working. If I put this code in my .htaccess file, it will work correctly, but due to external factors, I need to put this redirect into a separate .conf file. I have tried several different configurations, I researched a lot about it already, but nothing that could help me solve this problem. Thanks in advance!

I guess the easiest solution would be to drop the query string entirely?!
Note that QSD requires Apache 2.4 or newer.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^ %{REQUEST_URI} [QSD,L,R=301]
</IfModule>
Added a more generalized and easier to comprehend (for humans) version:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSD,L,R=301]
</IfModule>
.+ is the generalized form of and query string that isn't NULL. and the RewriteRule now lists all parts of the URL, but I'd stick to ^ %{REQUEST_URI} for the real version, because both versions just rewrite every URL to itself.

Related

Getting 404 error when viewing CakePHP 3 app on 1and1

When I attempt to hit my site which is at mydomain.com/subfolder, I get a 404 (from Apache).
/subfolder/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/subfolder/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To confirm that mod_rewrite works, I added this in the /subfolder/.htaccess file which promptly redirected:
Redirect 301 / http://other-domain-owned.com/
So, .htaccess are live and mod_rewrite works.
On my development environment, I was able to make the site work in the same /subfolder by symply enabling .htaccess files for that folder via a Directory directive.
Some tutorials say to add RewriteBase / but this didn't help.
Instead of RewriteBase, you can also use an absolute path. Additionally, the first two rules can be simplified to one
RewritRule ^(.*)$ /subfolder/webroot/$1 [L]
Similar the second part
RewriteRule ^ /subfolder/webroot/index.php [L]
Finally, Redirect and RewriteRule are from different modules. So, if one of the directives is working, this is no proof for the other one working too.

Block Access to Entire Folder and Instead Route to index.php

I'm trying to write a .htaccess rewrite rule to redirect all traffic coming to domain.com/system/* back to domain.com.
I've written this so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1/ [L]
But it's not working as expected, if I go to:
domain.com/system/inexistent_file
it works. But if I go to:
domain.com/system/existing_file.php
It will open the specified file instead of showing index.php contents.
How should I fix this?
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
# Match against the host, if you wish
#RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule system/(.*) /index.php?/$1 [L,R]
</IfModule>
It works for both existent and non-existent files.
The [R] flag is there because you stated that you want to redirect.

apache2 mod_rewrite per-directory request in rewritecond

My current .htaccess file works but I think that is not the best way of solving this problem and I'm afraid that it won't work in the future with more rewrite rules.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^files/(.*) files/$1 [END]
RewriteRule ^(.*) files/$1 [END]
</IfModule>
The server checks if the url leads to "files" folder and only rewrites if it leads somewhere else.
I tried something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule !^files/(.*) files/$1 [END]
</IfModule>
but it doesn't work since I can't pass $1 variable. I wan't something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{URL_STARTING_AT_THIS_POINT} ^files/*
RewriteRule ^(.*) files/$1 [END]
</IfModule>
or
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !%{URL_TO_THIS_HTACCESS}/files/*
RewriteRule ^(.*) files/$1 [END]
</IfModule>
I didn't find any server variables for this. I want to compare the first parameter from RewriteRule (^files/) in RewriteCond and I don't find the correct server variable.
I didn't find any server variables for this.
The server variable you are probably after is REQUEST_URI - which contains the URL-path, including the slash prefix. For example:
RewriteCond %{REQUEST_URI} !^/files/
RewriteRule (.*) files/$1 [L]
Or, you could simply write this:
RewriteRule !^files/ files%{REQUEST_URI} [L]
However, your original directives are probably OK, although could be tidied a bit:
RewriteRule ^files/(.*) - [L]
RewriteRule (.*) files/$1 [L]
The hyphen in the substitution can be used when there is no change to the URL. I don't think you really need the END flag here, unless you have other directives that might conflict?

Url renaming using .htaccess file

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!
I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteRule ^fileThathasalongname.html file.html

Folder control with .htaccess

I have a directory /public_html/myfolder/ and I want my domain www.example.com to point to /public_html/myfolder/ as my root folder.
I figured that out, but problem is, my images are not showing.
Here's my .htaccess file:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .* https://www.example.com/$1 [L,R=301]
RewriteRule ^$ myfolder/index.php [L]
# Require SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST} [L]
# Rewrite rules
<IfModule mod_rewrite.c>
#RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
</IfModule>
The issue may be that you have relative paths specified for your images on pages and after the URL is rewritten the links get broken. To fix this cosider using root-relative paths for the resources.
You will need to make sure the images are ignored by using RewriteCond's.
Try something like
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
(not sure if the . should be escaped, but i think it should be)
You can also try adding
RewriteLog
RewriteLogLevel 3 (or maybe greater)
I'm not sure if this works if it is inserted into a .htaccess, it might be that it has to be in the server conf, but it is worth a try. It is easier to debug rewrite if you have the log.