htaccess rewrite applying to all, not intended - regex

In my root .htaccess I have
Options +FollowSymlinks
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/dashboard/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^dashboard.domain.com
RewriteRule ^(.*)$ /dashboard/$1 [NC,L,NS]
In the dashboard folder's .htaccess I have..
RewriteRule ^go/([^/.]+)/?$ index.php?flag=$1 [L]
When I go to dashboard.domain.com/login it throws up an error.
When I go to dashboard.domain.com/go/a it works.
When I # out the dashboard rewrite rule the opposite occurs, /login works, but then /go/$1 doesn't. What am I missing to make the /go rewrite only apply when /go is detected?
EDIT (rest of root htaccess)
AddHandler application/x-httpd-php54 .php
Options +FollowSymlinks
Options -Indexes
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/rocket/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^launch.domain.com
RewriteRule ^(.*)$ /rocket/$1 [NC,L,NS]
RewriteCond %{REQUEST_URI} !^/dashboard/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^dashboard.domain.com
RewriteRule ^(.*)$ /dashboard/$1 [NC,L,NS]
RewriteCond %{REQUEST_URI} !^/img/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^img.domain.com
RewriteRule ^(.*)$ /img/$1 [NC,L,NS]
#RewriteCond %{HTTPS} off
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC]
#RewriteCond %{HTTP_HOST} !launch.domain.com [NC]
#RewriteRule ^(.*)$ https://launch.domain.com/$1 [R=301,L,QSA]
Redirect 301 /sneakpeek /
RewriteRule ^error/([^/]+) error/failure.php?code=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
<FilesMatch "\.(ttf|otf|eot|woff|jpg|png|jpeg|gif|js|json|html|css|php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
ErrorDocument 400 /error/400
ErrorDocument 401 /error/401
ErrorDocument 403 /error/403
ErrorDocument 404 /error/404
ErrorDocument 500 /error/500

Rules below your dashboard rule might be affecting your rule and changing %{REQUEST_URI} variable. You can use THE_REQUEST variable and retest it after clearing your browser cache:
RewriteCond %{THE_REQUEST} !/(dashboard|error)/
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} =dashboard.domain.com
RewriteRule ^(.*)$ dashboard/$1 [NC,L]

Related

apache .htaccess rewrite should not include www

We have a domain test.com, we want to redirect http://www.test.com to http://test.com and https://www.test.com to https://test.com, We can achieve the same by below mentioned rule, but we dont want http:// and beside what do we have to do for https://
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You can have a single rule to handle both http and https traffic:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L,NE]
If you want everything to go to https, then you need to change your rule to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
Otherwise, you'll need separate rules for http and https:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

.htaccess redirect everything to https non-www and 404 to index

I was trying to use .htaccess to redirect any request to https non-www and every 404 to index (previous rules included). This is the end result, but it doesn't work as intended. What am I doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 https://mydomain.com/
You can use this code in your .htaccess:
ErrorDocument 404 https://mydomain.com/
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://mydomain.com%{REQUEST_URI} [L,R=301,NE]
This should work:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://mydomain.com/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [R=301,L]
The above will redirect all non-https to https and then if the file doesn't exist it is redirect to the index.

.htacces without trailing slash redirect to with trailing slash

I'm using this .htaccess with this inside:
DirectoryIndex index.php index.html
DirectorySlash On
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(page1|page2|page3)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
#Show every html,htm and xml as php
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^(.*)\.htm$ $1.php [nc]
RewriteRule ^(.*)\.xml$ $1.php [nc]
but i did not get to redirect http://example.com/page1 to -> http://example.com/page1/ (and the others page2 and page3 ). since i have test in local my url will be
localhost/example/page1
and should redirect to
localhost/example/page1/
does any one see any problem?
For adding a trailing slash you can have your rules like this:
DirectoryIndex index.php index.html
DirectorySlash On
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(page1|page2|page3)$ /$1/ [R=301,L,NC]
#Show every html,htm and xml as php
RewriteRule ^(.+?)\.(?:xml|html?)$ $1.php [NC,L]

Rewriteconds won't work locally

I setted up a local environament using vagrant and virtualBox,
And some of this rules I have in my .htaccess (wich work in production code) will fire 404 error, when in production environament work perfectly
This one, for example works:
# hostname/posts/?show=lasts -> posts.php?show=lasts
RewriteRule ^posts/$ posts.php?$1&friendly=1 [QSA]
Won't work (404):
# hostname/page/the-title/5 -> page.php?id=5
RewriteRule ^page/(.+)/(.+) page.php?id=$2&friendly=1
Any idea what I'm missing?
hostname in production it's like page.com and in the virtual machine it's localhost:8085 in case it helps
-EDIT-
Full block:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$
RewriteRule ^(.*)$ /$1?%1-%2 [L,R=301,NE]
ErrorDocument 500 /oohps.php
ErrorDocument 404 /where.php
RewriteBase /
# Quitar www
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Canonicación de la IP
RewriteCond %{HTTP_HOST} ^70.XX.XX.XXX [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^s158783.gridserver.com$
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^new.domain.com$
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^beta.domain.com$
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# URLS amigables
RewriteRule ^page/(.+)/(.+) page.php?id=$2&friendly=1
RewriteRule ^pregunta/(.+)/(.+) pregunta.php?id=$2&friendly=1
RewriteRule ^new/(.+)/(.+) new.php?id=$2&friendly=1
RewriteRule ^user/(.+)/(.+) user.php?que=user&id=$2&friendly=1
RewriteRule ^pages-de-cocina/$ pages.php?$1&friendly=1 [QSA]
RewriteRule ^pages/(.+)/(.+) pages.php?que=cat&f=$2&friendly=1 [QSA]
URL's that fail:
/page/the-title/4
/user/the-name/6
Keep your rules like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^posts/(.*)$ posts.php?$1&friendly=1 [L,QSA,NC]
RewriteRule ^page/[^/]+/(.*)$ page.php?id=$1&friendly=1 [L,QSA,NC]

Redirect to Mobile Site with Full Site Link

I'm working on setting up a redirect to a mobile landing page for mobile users and the script I'm using is throwing up a 505.
Entire htaccess code is below: (Updated)
<FilesMatch "\.(html|htm|js|css)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>
#MobileSetup
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ – [CO=mredir:0:http://www.maindomain.com]
RewriteCond %{HTTP_HOST} !^http://www.maindomain.com/mobile/\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|css|png|gif)$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) [NC]
RewriteCond %{HTTP_COOKIE} !mredir=0 [NC]
RewriteRule ^ http://www.maindomain.com/mobile/? [R,L]
#Was at the top
RewriteEngine On
RewriteBase /
RewriteRule ^news/([a-zA-Z0-9_-]+)$ /news.php?url=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com$1 [R=301,L]
ErrorDocument 404 /404.php
#Redirects
Redirect 301 main site redirects
#10.28.13
Redirect 301 main site redirects
I've tried debugging this but to no avail. There is a redirect set up on the server that is forcing a non-www. to go to the www..
Could this be causing an issue?
Thanks for help.
Can you try:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ – [CO=mredir:0:http://www.regularsite.com]
RewriteCond %{HTTP_HOST} !^mobile-site\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|css|png|gif)$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) [NC]
RewriteCond %{HTTP_COOKIE} !mredir=0 [NC]
RewriteRule ^ http://mobile-site.com/? [R,L]
UPDATE:: Your full .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ – [CO=mredir:0:http://www.maindomain.com]
RewriteCond %{REQUEST_URI} !\.(jpg|css|png|gif)$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) [NC]
RewriteCond %{HTTP_COOKIE} !mredir=0 [NC]
RewriteRule !^mobile/ /mobile/? [R,L]
RewriteCond %{HTTP_HOST} ^maindomain.com$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^news/([\w-]+)$ /news.php?url=$1 [NC,L,QSA]