Mod_rewrite is adding /var/www to the resulting URL
up vote
1
down vote
favorite
I have some problems with the Apache mod_rewrite rules. Whenever I try to go to https://example.com// (see double slashes at the end) it redirects to a 301 page but it's adding the location of the directory, i.e. https://example.com/var/www/my-domain.com/html which is not desirable.
Here is my .htaccess file:
ErrorDocument 404 /views/pages/404.php
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
RewriteRule ^contact-us/?$ views/pages/contact.php [NC,L]
Same happens when I go to https://example.com//contact-us.
https://example.com/contact-us// is redirecting well to https://example.com/contact-us and https://example.com//contact-uss is redirecting well to the 404 page.
If one needs further information let me know.
apache .htaccess mod-rewrite
add a comment |
up vote
1
down vote
favorite
I have some problems with the Apache mod_rewrite rules. Whenever I try to go to https://example.com// (see double slashes at the end) it redirects to a 301 page but it's adding the location of the directory, i.e. https://example.com/var/www/my-domain.com/html which is not desirable.
Here is my .htaccess file:
ErrorDocument 404 /views/pages/404.php
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
RewriteRule ^contact-us/?$ views/pages/contact.php [NC,L]
Same happens when I go to https://example.com//contact-us.
https://example.com/contact-us// is redirecting well to https://example.com/contact-us and https://example.com//contact-uss is redirecting well to the 404 page.
If one needs further information let me know.
apache .htaccess mod-rewrite
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have some problems with the Apache mod_rewrite rules. Whenever I try to go to https://example.com// (see double slashes at the end) it redirects to a 301 page but it's adding the location of the directory, i.e. https://example.com/var/www/my-domain.com/html which is not desirable.
Here is my .htaccess file:
ErrorDocument 404 /views/pages/404.php
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
RewriteRule ^contact-us/?$ views/pages/contact.php [NC,L]
Same happens when I go to https://example.com//contact-us.
https://example.com/contact-us// is redirecting well to https://example.com/contact-us and https://example.com//contact-uss is redirecting well to the 404 page.
If one needs further information let me know.
apache .htaccess mod-rewrite
I have some problems with the Apache mod_rewrite rules. Whenever I try to go to https://example.com// (see double slashes at the end) it redirects to a 301 page but it's adding the location of the directory, i.e. https://example.com/var/www/my-domain.com/html which is not desirable.
Here is my .htaccess file:
ErrorDocument 404 /views/pages/404.php
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
RewriteRule ^contact-us/?$ views/pages/contact.php [NC,L]
Same happens when I go to https://example.com//contact-us.
https://example.com/contact-us// is redirecting well to https://example.com/contact-us and https://example.com//contact-uss is redirecting well to the 404 page.
If one needs further information let me know.
apache .htaccess mod-rewrite
apache .htaccess mod-rewrite
edited Nov 11 at 2:53
MrWhite
11.9k33059
11.9k33059
asked Nov 10 at 6:07
resizemyimg.com
82531837
82531837
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
You are missing the slash prefix on the substitution. This results in a relative-path substitution (since the $1 backreference does not contain the slash prefix), to which mod_rewrite prefixes the directory-prefix (ie. /var/www/example.com/html). This would result in the malformed redirect you are seeing. The RewriteRule should be written as:
RewriteRule (.*) /$1 [R=301,L]
(The ^ anchor on the RewriteRule pattern is unnecessary here.)
However, the following redirect is also invalid:
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
You are missing the substitution argument altogether. [L,R=404] will be seen as the substitution string (not the flags, as intended). This would also result in a malformed rewrite/redirect. The RewriteRule should be written as:
RewriteRule (.*) - [R=404]
Note the - (single hyphen) is used as the substitution argument (which is later ignored). When specifying a non-3xx response code, the L flag is implied.
However, I'm curious what it is you are trying to do here, as you appear to be "accepting" multiple slashes in one directive (by reducing them), but then rejecting multiple slashes in another directive (with a 404)? Why not reduce all sequences of multiple slashes wherever they occur in the URL-path?
For example, replace the following (modified code):
# Remove trailing slash from URL (except files and directories)
# >>> Why files? Files don't normally have trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Reject multiple slashes later in the URL or 3+ slashes at the start of the URL
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule (.*) - [R=404]
# Reduce multiple slashes at the start of the URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule (.*) /$1 [R=301,L]
With something like the following (depending on requirements):
# Reduce sequences of multiple slashes to a single slash in the URL-path
# NB: This won't work to reduce slashes in the query string (if that is an issue)
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) /$1 [R=302,L]
# Remove trailing slash from URL (except directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
Note that I've reversed the directives so that slashes are reduced before the final trailing slash is removed.
Test with 302s to avoid caching issues. And clear your browser cache before testing.
UPDATE: If double slashes could ever (legitimately) occur in the query string portion of the URL then the above will result in a redirect loop since the condition checks for multiple slashes anywhere in the URL (including the query string), whereas the RewriteRule only reduces multiple slashes in the URL-path. If you need to allow multiple slashes in the query string then change the CondPattern from //+ to s[^?]*//+ to specifically check the URL-path only, not the entire URL. In other words:
RewriteCond %{THE_REQUEST} s[^?]*//+
RewriteRule (.*) /$1 [R=302,L]
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
You are missing the slash prefix on the substitution. This results in a relative-path substitution (since the $1 backreference does not contain the slash prefix), to which mod_rewrite prefixes the directory-prefix (ie. /var/www/example.com/html). This would result in the malformed redirect you are seeing. The RewriteRule should be written as:
RewriteRule (.*) /$1 [R=301,L]
(The ^ anchor on the RewriteRule pattern is unnecessary here.)
However, the following redirect is also invalid:
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
You are missing the substitution argument altogether. [L,R=404] will be seen as the substitution string (not the flags, as intended). This would also result in a malformed rewrite/redirect. The RewriteRule should be written as:
RewriteRule (.*) - [R=404]
Note the - (single hyphen) is used as the substitution argument (which is later ignored). When specifying a non-3xx response code, the L flag is implied.
However, I'm curious what it is you are trying to do here, as you appear to be "accepting" multiple slashes in one directive (by reducing them), but then rejecting multiple slashes in another directive (with a 404)? Why not reduce all sequences of multiple slashes wherever they occur in the URL-path?
For example, replace the following (modified code):
# Remove trailing slash from URL (except files and directories)
# >>> Why files? Files don't normally have trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Reject multiple slashes later in the URL or 3+ slashes at the start of the URL
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule (.*) - [R=404]
# Reduce multiple slashes at the start of the URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule (.*) /$1 [R=301,L]
With something like the following (depending on requirements):
# Reduce sequences of multiple slashes to a single slash in the URL-path
# NB: This won't work to reduce slashes in the query string (if that is an issue)
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) /$1 [R=302,L]
# Remove trailing slash from URL (except directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
Note that I've reversed the directives so that slashes are reduced before the final trailing slash is removed.
Test with 302s to avoid caching issues. And clear your browser cache before testing.
UPDATE: If double slashes could ever (legitimately) occur in the query string portion of the URL then the above will result in a redirect loop since the condition checks for multiple slashes anywhere in the URL (including the query string), whereas the RewriteRule only reduces multiple slashes in the URL-path. If you need to allow multiple slashes in the query string then change the CondPattern from //+ to s[^?]*//+ to specifically check the URL-path only, not the entire URL. In other words:
RewriteCond %{THE_REQUEST} s[^?]*//+
RewriteRule (.*) /$1 [R=302,L]
add a comment |
up vote
1
down vote
accepted
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
You are missing the slash prefix on the substitution. This results in a relative-path substitution (since the $1 backreference does not contain the slash prefix), to which mod_rewrite prefixes the directory-prefix (ie. /var/www/example.com/html). This would result in the malformed redirect you are seeing. The RewriteRule should be written as:
RewriteRule (.*) /$1 [R=301,L]
(The ^ anchor on the RewriteRule pattern is unnecessary here.)
However, the following redirect is also invalid:
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
You are missing the substitution argument altogether. [L,R=404] will be seen as the substitution string (not the flags, as intended). This would also result in a malformed rewrite/redirect. The RewriteRule should be written as:
RewriteRule (.*) - [R=404]
Note the - (single hyphen) is used as the substitution argument (which is later ignored). When specifying a non-3xx response code, the L flag is implied.
However, I'm curious what it is you are trying to do here, as you appear to be "accepting" multiple slashes in one directive (by reducing them), but then rejecting multiple slashes in another directive (with a 404)? Why not reduce all sequences of multiple slashes wherever they occur in the URL-path?
For example, replace the following (modified code):
# Remove trailing slash from URL (except files and directories)
# >>> Why files? Files don't normally have trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Reject multiple slashes later in the URL or 3+ slashes at the start of the URL
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule (.*) - [R=404]
# Reduce multiple slashes at the start of the URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule (.*) /$1 [R=301,L]
With something like the following (depending on requirements):
# Reduce sequences of multiple slashes to a single slash in the URL-path
# NB: This won't work to reduce slashes in the query string (if that is an issue)
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) /$1 [R=302,L]
# Remove trailing slash from URL (except directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
Note that I've reversed the directives so that slashes are reduced before the final trailing slash is removed.
Test with 302s to avoid caching issues. And clear your browser cache before testing.
UPDATE: If double slashes could ever (legitimately) occur in the query string portion of the URL then the above will result in a redirect loop since the condition checks for multiple slashes anywhere in the URL (including the query string), whereas the RewriteRule only reduces multiple slashes in the URL-path. If you need to allow multiple slashes in the query string then change the CondPattern from //+ to s[^?]*//+ to specifically check the URL-path only, not the entire URL. In other words:
RewriteCond %{THE_REQUEST} s[^?]*//+
RewriteRule (.*) /$1 [R=302,L]
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
You are missing the slash prefix on the substitution. This results in a relative-path substitution (since the $1 backreference does not contain the slash prefix), to which mod_rewrite prefixes the directory-prefix (ie. /var/www/example.com/html). This would result in the malformed redirect you are seeing. The RewriteRule should be written as:
RewriteRule (.*) /$1 [R=301,L]
(The ^ anchor on the RewriteRule pattern is unnecessary here.)
However, the following redirect is also invalid:
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
You are missing the substitution argument altogether. [L,R=404] will be seen as the substitution string (not the flags, as intended). This would also result in a malformed rewrite/redirect. The RewriteRule should be written as:
RewriteRule (.*) - [R=404]
Note the - (single hyphen) is used as the substitution argument (which is later ignored). When specifying a non-3xx response code, the L flag is implied.
However, I'm curious what it is you are trying to do here, as you appear to be "accepting" multiple slashes in one directive (by reducing them), but then rejecting multiple slashes in another directive (with a 404)? Why not reduce all sequences of multiple slashes wherever they occur in the URL-path?
For example, replace the following (modified code):
# Remove trailing slash from URL (except files and directories)
# >>> Why files? Files don't normally have trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Reject multiple slashes later in the URL or 3+ slashes at the start of the URL
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule (.*) - [R=404]
# Reduce multiple slashes at the start of the URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule (.*) /$1 [R=301,L]
With something like the following (depending on requirements):
# Reduce sequences of multiple slashes to a single slash in the URL-path
# NB: This won't work to reduce slashes in the query string (if that is an issue)
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) /$1 [R=302,L]
# Remove trailing slash from URL (except directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
Note that I've reversed the directives so that slashes are reduced before the final trailing slash is removed.
Test with 302s to avoid caching issues. And clear your browser cache before testing.
UPDATE: If double slashes could ever (legitimately) occur in the query string portion of the URL then the above will result in a redirect loop since the condition checks for multiple slashes anywhere in the URL (including the query string), whereas the RewriteRule only reduces multiple slashes in the URL-path. If you need to allow multiple slashes in the query string then change the CondPattern from //+ to s[^?]*//+ to specifically check the URL-path only, not the entire URL. In other words:
RewriteCond %{THE_REQUEST} s[^?]*//+
RewriteRule (.*) /$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
You are missing the slash prefix on the substitution. This results in a relative-path substitution (since the $1 backreference does not contain the slash prefix), to which mod_rewrite prefixes the directory-prefix (ie. /var/www/example.com/html). This would result in the malformed redirect you are seeing. The RewriteRule should be written as:
RewriteRule (.*) /$1 [R=301,L]
(The ^ anchor on the RewriteRule pattern is unnecessary here.)
However, the following redirect is also invalid:
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule ^(.*) [L,R=404]
You are missing the substitution argument altogether. [L,R=404] will be seen as the substitution string (not the flags, as intended). This would also result in a malformed rewrite/redirect. The RewriteRule should be written as:
RewriteRule (.*) - [R=404]
Note the - (single hyphen) is used as the substitution argument (which is later ignored). When specifying a non-3xx response code, the L flag is implied.
However, I'm curious what it is you are trying to do here, as you appear to be "accepting" multiple slashes in one directive (by reducing them), but then rejecting multiple slashes in another directive (with a 404)? Why not reduce all sequences of multiple slashes wherever they occur in the URL-path?
For example, replace the following (modified code):
# Remove trailing slash from URL (except files and directories)
# >>> Why files? Files don't normally have trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Reject multiple slashes later in the URL or 3+ slashes at the start of the URL
RewriteCond %{THE_REQUEST} s/+(.*?)/+(/S+) [NC]
RewriteRule (.*) - [R=404]
# Reduce multiple slashes at the start of the URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/{2,} [NC]
RewriteRule (.*) /$1 [R=301,L]
With something like the following (depending on requirements):
# Reduce sequences of multiple slashes to a single slash in the URL-path
# NB: This won't work to reduce slashes in the query string (if that is an issue)
RewriteCond %{THE_REQUEST} //+
RewriteRule (.*) /$1 [R=302,L]
# Remove trailing slash from URL (except directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]
Note that I've reversed the directives so that slashes are reduced before the final trailing slash is removed.
Test with 302s to avoid caching issues. And clear your browser cache before testing.
UPDATE: If double slashes could ever (legitimately) occur in the query string portion of the URL then the above will result in a redirect loop since the condition checks for multiple slashes anywhere in the URL (including the query string), whereas the RewriteRule only reduces multiple slashes in the URL-path. If you need to allow multiple slashes in the query string then change the CondPattern from //+ to s[^?]*//+ to specifically check the URL-path only, not the entire URL. In other words:
RewriteCond %{THE_REQUEST} s[^?]*//+
RewriteRule (.*) /$1 [R=302,L]
edited Nov 13 at 0:12
answered Nov 11 at 2:50
MrWhite
11.9k33059
11.9k33059
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236444%2fmod-rewrite-is-adding-var-www-to-the-resulting-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown