htaccess remove www from URL respect RewriteBase - .htaccess

In base directory it works fine, but if you go in subdirectory: example www.domain.com/dir/
used RewriteBase is lost.
In htaccess I have something like this....
Options +FollowSymLinks
RewriteEngine On
RewriteBase /dir/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteRule ^home/?$ index.php [L]
So if we put www.domain.com/dir/home it redirect us to http://domain.com/home and /dir/ is missing ...
What I is wrong ... Thanks for ideas.

Just put dir in Rule
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/dir/$1 [L,R=301]

Related

Root htaccess overriding the sub folder htaccess

I have a htaccess file in root and another htaccess in films folder. I want to stop all the rewrite urls or conditions to affect the films folder. I have some other rules in film folder htaccess.
My root htaccess looks like this
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^abc\.in$ [NC]
RewriteRule ^(.*)$ http://www.abc.in/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
My sub directory films folder htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /films/
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^change-password/$ changepassword.php
RewriteRule ^change-password$ changepassword.php
The above code is just a glimpse of my original htaccess
You can do it by following code :-
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/films/$1 [R=301,L]
It may help you.
In /films/.htaccess ,Change your rule
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
to this :
RewriteRule ^(.*)$ http://%1/films/$1 [R=301,L]
To redirect all /films requests to www, you can use something like the following :
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Need to correct htaccess to restrict some folder

I am using following htaccess code in my project
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
#RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^ /%1.html [R=302,L,NE]
#RewriteRule ^(.+?)\.html$ $1.php [L,NC]
Now everything is working fine except one thing I have some webservice also that I am calling from webservice folder, url is
http://localhost/apnaujjain/webservice/gethomepagecontent1.php
Now the problem is due to htaccess my webservice stopped working, it redirecting to wrong page so its not working. I don't want to apply htaccess rule on webservice folder. Please help, thanks in advance.
Just below RewriteBase /apnaujjain line add this line:
RewriteEngine On
RewriteBase /apnaujjain/
# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]
# rest of your existing rules

htaccess adding query string to 301 redirect

Can sombody help me out with this, im trying to re direct a page using htaccess file but it keeps adding ?c=oldpage on to the end of the new url, example:
http://www.mydomain.co.uk/newpage.html?c=oldpage
i have tried some of the solutions posted here but no luck, here is my .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteEngine on
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
Redirect 301 /oldpage.html http://www.mydomain.co.uk/newpage.html
ErrorDocument 404 /404.php
Thanks for any help.
This is mod_alias (the Redirect directive) and mod_rewrite not playing nicely with each other. Because both modules apply their directives on the same URI in the URL-file mapping pipeline, they don't know to ignore each other since neither directive knows what the other module is doing. Since you're targets overlap, both modules are applying their directives on the same URI and you get a mish-mashed result.
You need to stick with mod_rewrite in this case and move the redirect above the internal rewrites:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
# redirects
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^oldpage.html$ http://www.mydomain.co.uk/newpage.html [R=301,L]
# internal rewrites
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\/" [R=301,L]
RewriteOptions inherit
to a folder, or:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\.html$" [R=301,L]
RewriteOptions inherit
Don't know much about it, but it's what i use, hope it helps.

how to ignore the .htaccess file from a parent directory

I'm using rewrite rules for search engine optimized url's
In my root folder I have the following .htaccess file:
Options +FollowSymlinks RewriteEngine On RewriteCond %{http_host} ^elitegameservers.net [NC]
RewriteRule ^(.*)$ http://www.elitegameservers.net/$1 [R=301,L]
rewriteEngine on
rewriteBase /
rewriteCond %{HTTP_HOST} ^(.*)haloservers(.nl|.us|.net)$
rewriteRule ^(.*) http://www.haloservers.net/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?haloservers(.nl|.us|.net) [NC]
RewriteRule ^(.*)\.html$ http://www.elitegameservers.net/$1 [R=301,L]
In the sub directory(/controlpanel) I run WHMCS with the following .htaccess file:
RewriteEngine On
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^elitegameservers.net [NC]
RewriteRule ^(.*)$ http://www.elitegameservers.net/controlpanel/$1 [R=301,L]
RewriteEngine On
# Announcements
RewriteRule ^announcements/([0-9]+)/[a-z0-9_-]+\.html$ ./announcements.php?id=$1 [L,NC]
RewriteRule ^announcements$ ./announcements.php [L,NC]
# Downloads
RewriteRule ^downloads/([0-9]+)/([^/]*)$ ./downloads.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^downloads$ ./downloads.php [L,NC]
# Knowledgebase
RewriteRule ^knowledgebase/([0-9]+)/[a-z0-9_-]+\.html$ ./knowledgebase.php?action=displayarticle&id=$1 [L,NC]
RewriteRule ^knowledgebase/([0-9]+)/([^/]*)$ ./knowledgebase.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^knowledgebase$ ./knowledgebase.php [L,NC]
I noticed that knowledgeable SEO optimization in WHMCS doesn't seem to work properly and it seems to be caused by the .htaccess code in my root directory.
Because of that I want the controlpanel sub directory to be excluded from the code in the root directory.
Thanks for any help
You could try adding a condition to the rules in your root directory to exclude requests starting with controlpanel:
RewriteCond %{REQUEST_URI} !^/controlpanel
rewriteCond %{HTTP_HOST} ^(.*)haloservers(.nl|.us|.net)$
rewriteRule ^(.*) http://www.haloservers.net/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/controlpanel
RewriteCond %{HTTP_HOST} ^(www\.)?haloservers(.nl|.us|.net) [NC]
RewriteRule ^(.*)\.html$ http://www.elitegameservers.net/$1 [R=301,L]

htaccess change directory

Hi i need to use this existing htaccess located on the root folder.
I now need to create a new directory /testfolder/ to become http://www.companydomain.com/testfolder/ where all the files of the root will be located.
How can I make this happen with this existing htaccess? Much appreciated your help in advance!
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !(.*)/(.*).html$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
# 301
RewriteCond %{HTTP_HOST} !^www\.companydomain\.com$ [NC]
RewriteRule .? http://www.companydomain.com%{REQUEST_URI} [R=301,L]
RewriteRule ^Sportsbooks\/(.*).html$ Sportsbooks\/$1\/ [R=301,L]
This .htaccess file will redirect http://example.com/file.html to http://example.com/folder1/file.html:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !folder1
RewriteRule ^(.*)$ http://example.com/folder1/$1 [R=301,L]
I hope this helps!

Resources