Can't access permalink without www in front - .htaccess

I have a plugin to set up permalinks on my site however it only works when lead by www in front of the address. All other site addresses function fine with our without the leading www. I've been playing around my htaccess file but can't figure out how to fix this. The first portion is generated by the plugin, while I inserted the last 2 lines. Any suggestions on how to fix this? The last condition on my file seems to be getting ignored by all the permalinks.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^permalinks_dispatcher\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/plugins/permalinks_dispatcher.php [L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
</IfModule>

You only have to begin with your www-rule first
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^permalinks_dispatcher\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /cms/plugins/permalinks_dispatcher.php [L]
</IfModule>

Related

how to mask part of URL after doing a mod_rewrite

I have several client domains I would like to point to my droplet(I have a vue app here) and serve the right path depending on the domain being called.
eg:
domainA.com should go to mysite.com/path-a
domainB.com should go to mysite.com/path-b
I have tried to rewrite the URL using .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} =domainA
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ %{HOST_NAME}/uri-a [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
However, I can't seem to be able to remove the URI and just show the client domain name.
Would also like to make it dynamic so I don't have to write the same rule for each client domain
Could you please try following, based on your shown samples, please clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-a [R=301,L]
RewriteCond %{HTTP_HOST} ^domainB\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-b [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
You may try this code in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^$ path-a [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainB\.com$ [NC]
RewriteRule ^$ path-b [L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
Make sure to test from a new browser or after clearing your browser cache.

Htaccess rewrite / redirect issue from .png.webp back to png

Can any experts in .htaccess tell me why this isn't working? (it's the last line that is key - the .png.webp to png rewrite) Please help!
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.test.com [NC]
RewriteRule ^(.*)$ https://test.com/$1 [L,R=301]
RewriteRule ^(.*)\.webp.png$ $1.png [L,R=301]
Example issue:
https://centiqsap.com/wp-content/uploads-webpc/uploads/2020/07/Group-13-min-2.png.webp
Needs to rewrite and redirect to
https://centiqsap.com/wp-content/uploads-webpc/uploads/2020/07/Group-13-min-2.png
Have your htaccess file like this way once(I have added new rule and kept OP's old rules too in rules file). Please make sure you clear your cache before testing your URLs. I have also fixed some minor issues, like your https redirection rule should be at very first and you should exacpe .(dots) in regex too.
RewriteEngine ON
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.test\.com [NC]
RewriteRule ^(.*)$ https://test\.com/$1 [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.*\.png)\.webp/?$ $1 [R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.webp -f
RewriteRule ^(.*)/?$ $1.webp [NC,L]

Wildcard Sub-Domain Setup

This works for rewriting all requests to the index.php located in my root folder, which works for example.com/1/2/3/4/5/...
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.mallzones\.com$
RewriteRule (.*) /%{REQUEST_URI}
I am trying to figure how to rewrite the wildcard subdomains as well. The wildcard should be prepended to the path. How do I modify these rules to accomplish the following url scheme.
example.com/1/ is equal to 1.example.com
example.com/1/2 is equal to 1.example.com/2
example.com/1/2/3 is equal to 1.example.com/2/3
example.com/1/2/3/4 is equal to 1.example.com/2/3/4
I've already created the A record in my DNS zones. What do I do next?
ANSWER: kudos to #CRIMSON 501
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ / [L]
Let me understand. Are you asking how to make it so that when a user navigates to example.com/1/ they are rerouted to 1.example.com?
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule (.*) http://example.com/ [L]
</IfModule>
To have a silent redirect the last line should be:
RewriteRule (.*) /index.php/or/whatever/you/need [L]
Just edit the links to what you need!

.htaccess retrieve from folder if subdomain, not 301 redirecting?

I want my site to just point to /user folder if the request is a subdomain.
If the request is subdomain.site.com/admin, then the site should show the page for subdomain.site.com/user/admin.
The problem with my code is that it makes an 301 redirect instead of just keeping the url-address.
My code look like this:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)
RewriteRule ^$ user/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) user/$1 [L]
</IfModule>
And you can try for yourself, go to http://mickes.bildsida.se/admin and see how the address changes to /user/admin. Very disturbing...
You just need a few of the lines you showed to get this working.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
RewriteRule ^(.*)$ user/$1 [L]
I adjusted the HTTP_HOST checks because that parameter only looks at the domain name, so you don't need the (.*) at the end. I also removed the checks that look if the file exists or is a directory, since you want everything redirected (no reason to make it possible to access files from other subdomains, for example)
You code can be simplified to these lines : (try them instead)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
RewriteRule ^(.*)$ /user/$1 [QSA,L]
Finally! After days and nights of reading forums and documentations, and testing all possible ways, I got it to work!
Sulotion:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} !^bildsida.se
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ user/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? user/$1/ [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.+)/ user/$1 [QSA]
</IfModule>

htaccess - redirect to ignore nested subdirectory and if possible make entire site non-www

I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)
Here's the situation:
I've got 1 main domain with 3 subdirectories that are nested within each other
(from top to bottom)
http://main-domain.com
then
http://main-domain.com/company-name/
then
http://main-domain.com/company-name/blog/
There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.
What's the problem?
Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever
So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/
I hope that the following examples will help to illustrate the point.
http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php
http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/
Why do I need this?
I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.
Here are My Current 3 htaccess files
root htaccess: main-domain.com
#Bypass InoCore Templating System
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /reservations/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]
Options -Indexes
</IfModule>
#END Bypass
#301 REDIRECT
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
company-name htaccess: main-domain.com/company-name/
RewriteEngine on
RewriteRule ^maping.php /maping.php
RewriteRule ^$ index.php?$1 [L]
RewriteRule (.*) index.php?$1 [L]
#php_flag magic_quotes_gpc off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]
</IfModule>
#END WordPress
blog htaccess: main-domain.com/company-name/blog/
RewriteEngine off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]
</IfModule>
#END WordPress
Your correct and compact root .htaccess should be like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
NC is for ignore case comparison
$1 is your REQUEST_URI matching group

Resources