Rewriteconds won't work locally - regex

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]

Related

htaccess rewrite applying to all, not intended

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]

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 from www to non-www

I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
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]

mod_rewrite redirects only index page

The problem is that following .htaccess syntax:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} robots.txt$ [NC]
RewriteRule ^([^/]+) $1 [L]
RewriteCond %{HTTP_HOST} ^bd-spb\.ru$ [NC]
RewriteRule ^(.*)$ http://xn----8sbbkdenhe5aqs7a.xn--p1ai/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.bd-spb\.ru$ [NC]
RewriteRule ^(.*)$ http://xn----8sbbkdenhe5aqs7a.xn--p1ai/$1 [R=301,L]
Redirects only index page. So if I go to bd-spb.ru I will get бизнес-диалог.рф, but if I go for example here its not redirects me here.
How to make this happens?
Ok replace your code with this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} robots\.txt$ [NC]
RewriteRule ^([^/]+) $1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?bd-spb\.ru$ [NC]
RewriteRule ^(.*)$ http://xn----8sbbkdenhe5aqs7a.xn--p1ai/$1 [R=301,L,B]

403 url rewrite error issue

Hi guys I a major issue with url rewrite. Apologizes if you might have seen this somewhere before.
issue here
If i enter a url for example exampl.x10.mx OR www.example.x10.mx I get a 403 error which shouldnt happen because
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
is MIGHT to take care of that.
RewriteCond %{REQUEST_URI} !^lwh/
RewriteCond $1 !^lwh/
The code above hiden the lwh folder.
FULL .htaccess CODE
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^lwh/
RewriteCond $1 !^lwh/
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule (.*) /lwh/main/pages/general/$1 [L]
RewriteRule ^(.*)$ lwh/$1 [L]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
Summary of the problem
If i remember
RewriteCond %{REQUEST_URI} !^lwh/
RewriteRule ^(.*)$ lwh/$1 [L]
the code below works and the same happens if i remember the code below. The thing is I need both of them.
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
An idea why this is happening please
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.x10\.mx$ [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=302,L]
RewriteCond %{REQUEST_URI} !^/lwh/
RewriteCond %{HTTP_HOST} ^example\.x10\.mx$ [NC]
RewriteRule ^(.*)$ /lwh/main/pages/general/$1 [L]
The problem was with the
R=301 (permanent redirect to new URL)
Before
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=302,L]
now
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [L]