403 url rewrite error issue - regex

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]

Related

How to remove subdirectory using rewriterule with non-www redirection

So I've been fiddling with .htaccess and I was able to remove both removing a subdirectory and having a non-www subdirection. Please find code below:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*) http://domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)subdir
RewriteRule ^(.*)$ subdir/$1 [L]
This can redirect:
www.domain.com/subdir/file
or
www.domain.com/file
to
domain.com/file
However, it can't redirect
domain.com/subdir/file
to
domain.com/file
That's my only problem with this code. Could somebody enlighten me what am I doing wrong? Any help is much appreciated. :)
You can use an OR condition in conditions:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{THE_REQUEST} /subdir/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:subdir/)?(.*)$ http://%1/$1 [NC,R=302,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]

htaccess Rewrite Rule not working on certain pages

im having some difficulty with Rewrite Rules. My .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule admin/add-category$ adm_add_category.php [NC]
RewriteRule admin/categories-management$ adm_categories_management.php [NC]
RewriteRule admin/user-settings/(.*)$ adm_user_settings.php?update=$1 [NC]
RewriteRule admin/website-settings/(.*)$ adm_website_settings.php?update=$1 [NC]
RewriteRule admin/users-management$ adm_users_management.php [NC]
RewriteRule admin/websites-management$ adm_websites_management.php [NC]
RewriteRule settings/profile$ changesettings.php [NC]
RewriteRule settings/websites$ my_websites.php [NC]
RewriteRule settings/password$ changepassword.php [NC]
RewriteRule settings/logout$ logout.php [NC]
RewriteRule pages/index$ index.php [NC]
RewriteRule pages/access$ access.php [NC]
RewriteRule pages/submit-url$ submit_url.php [NC]
RewriteRule pages/online-users$ online_users.php [NC]
RewriteRule pages/register$ register.php [NC]
RewriteRule pages/websites$ websites_list.php [NC]
RewriteRule pages/websites/(.*)$ websites_list.php?page=$1 [NC]
RewriteRule pages/contact$ contact.php [NC]
RewriteRule search/(.*)$ search.php?term=$1 [NC]
RewriteRule category/(.*)$ category.php?name=$1 [NC]
RewriteRule profile/(.*)$ profile.php?username=$1 [NC]
RewriteRule website/(.*)$ website.php?name=$1 [NC]
All of the rewrites except for the following are working:
RewriteRule search/(.*)$ search.php?term=$1 [NC]
RewriteRule category/(.*)$ category.php?name=$1 [NC]
RewriteRule profile/(.*)$ profile.php?username=$1 [NC]
RewriteRule website/(.*)$ website.php?name=$1 [NC]
When trying to load a specific profile, such as /profile/exampleguy, a 404 error is shown. Does anyone know what would cause this?
The 404 looks like:
The requested URL /profile/exampleguy was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Let me know if you need more information, i really appreciate the help!
Add L flag in each rule to make it [NC,L] and then add:
Options +FollowSymLinks -MultiViews
at top of .htaccess
Have you tried working in some rewrite conditions to redirect to a default page?
RewriteCond %{REQUEST_FILENAME} !-d //Rewrites if the directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f //Rewrites if the file doesn't exist
RewriteRule ^(.*)$ index.php?$1 [L,QSA] // Change this to suit your needs
Also, I would surmise you are getting the 404 because directories like /profile/ actually exists, but not an "exampleguy" and if there isn't another .htaccess in those directories to catch the issue then the server will try to find the page (and fail....)

Rewrite subdomain exactly like the main website

I'm currently working on a blog portal, and I'm trying to achieve a specific thing here.
This is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php? page=$1&page2=$2&page3=$3
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2&page3=$3
When a person is trying to reach SOMETHING.mydomain.com, htaccess is rewriting it to blogdisplay.php?page=BLOGNAME. Just as planned. Now, the problem is that I want it to behave exactly like the main website and rewrite SOMETHING.mydomain.com/page to blogdisplay.php?page=BLOGNAME?page2=page.
Is there any way to make the subdomain use blogdisplay.php instead of index.php, but otherwise work exactly like the subpages of the "main website"?
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]

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]