In the public_html folder which is the root folder of my main site thesite.com, I have this htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^m\.thesite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^blog\.thesite\.com$ [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.thesite\.com [NC]
RewriteRule ^(.*)$ https://thesite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
Here, am forcing non-www url as well as forcing https URL for the thesite.com. And I have two subdomains: m.thesite.com and blog.thesite.com.
Both are in separate subfolders inside public_html.
The m.thesite.com is in /public_html/m.thesite.com/ folder. And it has this htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.m\.thesite\.com [NC]
RewriteRule ^(.*)$ https://m.zapteka.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
For this subdomain also, I need to force https urls as well as non-www urls.
It seems to be working for the main site. I mean thesite.com. But when I try to access the m.thesite.com, it seems to be internally redirecting to the files of thesite.com instead of the m.thesite.com. I mean the url is being shown as https://m.thesite.com/ but the pages are from https://thesite.com
I have been trying to debug the issue for several hours now. Probably it could be something small that I missed. Any ideas?
BTW, thesite.com is having Wildcard SSL installed.
EDIT
Am sorry if my explanation was confusing. When I said url is showing as https://m.thesite.com/ but the content is from https://thesite.com/, I meant to say that in browser the url is shown as https://m.thesite.com. But the template file contents where from the desktop version of the main site thesite.com. The m.thesite.com is mobile version of the site. The pages that displays the contents where separate in both the desktop and mobile version of the site.
Like you see in the htaccess code that I included above, there is some PHP files inside the /public_html/ folder for the desktop version. All the files for the mobile version of the site is in /public_html/m.thesite.com/ folder.
So, in short, when I try to access the https://m.thesite.com/, it is displaying the index.php page from /public_html/index.php instead of /public_html/m.thesite.com/
So that's how I found that there's issue in the routing.
EDIT
VirtualHost entries fetched by using this command /usr/local/apache/bin/httpd -S:
I have also checked the Apache version and saw that it's 2.2.27 using the command: httpd -V
So I tried adding this line in the htaccess file:
RewriteLog "/logs/rewrite.log"
RewriteLogLevel 9
But it was giving internal server error. So I tried this line instead also:
LogLevel mod_rewrite.c:trace8
That was also giving me internal server error when site is accessed. So I commented it out.
Related
I am having difficulty configuring a mod_rewrite rule in my .htaccess file to hide certain subdirectories.
Input:
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
Desired output:
https://example.com/en-US/additional_sub/file
I had code that allowed me to hide something if I specifically name it:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /hidden_sub1/hidden_sub2/ [NC]
RewriteRule ^hidden_sub1/hidden_sub2/(.*) /$1 [L,R=301]
RewriteRule !^hidden_sub1/hidden_sub2/ hidden_sub1/hidden_sub2%{REQUEST_URI} [L]
However, it stopped working once I added in the following language subdirectory code:
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
Besides, the name of the subdirectory is dynamic. "hidden_sub1" might be "random_name243". However, they would always be the first two subdirectories.
So how do I take out the first two randomly-named subdirectories while still keeping the language subdirectory?
I appreciate any help.
To change your url from the form
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
To the form :
https://example.com/en-US/additional_sub/file
You can use the following Rewrite rule
RewriteEngine On
RewriteBase /
#1)externally redirect "/hidden_sub1/hidden_sub2/foo.php?lang=bar" to "/bar/foo"
RewriteCond %{THE_REQUEST} /hidden_sub1/hidden_sub2/([^.]+)\.php\?lang=(.+)\sHTTP [NC]
RewriteRule ^ /%2/%1? [L,R=301]
#2)internally rewrite "/bar/foo" to "/hidden_sub1/hidden_sub2/foo.php?lang=bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /hidden_sub1/hidden_sub2/$2.php?lang=$1 [NC,L]
Tested on apache 2.2. and 2.4.
i tried and tried with examples posted here, but i didn't manage to make my htaccess run properly.
Here's the situation:
i have links looking like this
domain.com/sport/football/index.php?lang_id=1&page_id=500 (home page)
domain.com/sport/football/index.php?lang_id=1&page_id=505 (players)
domain.com/sport/football/index.php?lang_id=1&page_id=510 (coaches) ...
i would like to rename them to
domain.com/sport/football/
domain.com/sport/football/players/
domain.com/sport/football/coaches/
etc...
and for all non-designated page_id's to redirect to home page.
All help is very much appreciated.
In the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^sport/football/$ /sport/football/index.php?lang_id=1&page_id=500 [L,QSA]
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L,QSA]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=500($|&)
RewriteRule ^ /sport/football/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=505($|&)
RewriteRule ^ /sport/football/players/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=510($|&)
RewriteRule ^ /sport/football/coaches/? [L,R=301]
You can use RewriteMap Directive for that. You must define the mapping from names to ids
players 505
coaches 510
and tell Apache about the map
RewriteMap football txt:/path/to/footballmap.txt
The RewriteMap must be in either the main configuration file or inside a VirtualHost directive.
Now you can use this map
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/(.*)/?$ /sport/football/index.php?lang_id=1&page_id=${footballmap:$1|500} [L]
If there's no key found, the default 500 (homepage) will be used. If you have lots of mappings, you can also use a hashfile instead.
Update:
When you don't have access to the server or virtual host configuration file, you can only have a fixed RewriteRule "map"
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L]
# maybe other similar rules ...
# this is a catch everything else and must come last
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/ /sport/football/index.php?lang_id=1&page_id=500 [L]
I am trying to implement 301's for sub-pages on my site which is hosted on an Apache server (Fasthosts), using Rewrite scripts in the .htaccess file. I have tried to follow many pieces of documentation (infact, I've never had any problems implementing 301's using .htaccess before!) but on this particular website, nothing seems to work.
There is currently a 301 re-direct in there from non-www to www which is working fine. There are also some other snippets using regex which I imagine are for the CMS.
Below is the current state of the .htaccess file. An example of a 301 I'm trying to re-direct is in there (Lines 5 & 6)
Old page: http://www.junkwize.com/home-Garden%20Clearance
to New page: http://www.junkwize.com/services/garden-clearance-london
.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^junkwize.com
RewriteRule (.*) http://www.junkwize.com/$1 [R=301]
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
#Options +FollowSymlinks
RewriteRule ^.htaccess$ — [F]
RewriteRule ^([/admin]+)$ admin/login.php [L]
RewriteRule ^([/admin]+)([/blocks]+)$ admin/login.php [L]
RewriteRule ^([/blocks]+)$ index.php [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)-([^/\.]+)$ index.php?main=$1&id=$2&menu=$3 [L]
RewriteRule ^([/deals]+)-([^/\.]+)$ index.php?main=$1&id=$2 [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?main=$1 [L]
Any help would be much appreciated.
Many thanks.
This code is faulty and is not going to work:
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
Reason is that RewriteCond %{HTTP_HOST} can only match host name. Replace that code with this:
RewriteRule ^home-Garden\ Clearance/?$ /services/garden-clearance-london [R=301,L,NC]
Situation
I'm using Zend framework, and thus attempt to call the folder by address ends in a fiasco and we gets the error: 'Invalid controller specified'. I needed to hook up additional forum to application in a separate folder. I change .htaccess file as follow:
RewriteRule ^forum(.*)$ forum$1 [L]
and for a while it was good, until I realized the fact that the page exists in two versions in two domains (php recognizes domain [.pl/.co.uk] and selects the language). So I decided to separate forums as a two separate modules (for example, the folder for Polish version forum will be: forum_pl, and for UK it will be: forum_uk - two different forums)
What's the problem?
It needs to rewrite .htaccess file to work like this:
IF: www.domena.pl/forum
THEN: open to the script from the folder /forum_pl
IF: www.domain.co.uk/forum
THEN: open to the script from the folder /forum_uk.
The worst thing is that when I tried to fix .htaccess file adding RewriteCond then the first redirect no longer work correctly, even after returning to the original (shown above) version.
Please help me and sorry if I did not find answers already given earlier.
EDIT:
First working solution
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It's look like it wants to work but, problems that needs solution is (TODO):
When you type: www.domain.pl/forum it gets you to www.domain.pl/forum_pl/ but if you type: www.domain.pl/forum/ it gets you to www.domain.pl/forum/ (read from forum_pl). How to make that it works in first situation same, as in second.
EDIT:
My last solution:
#FORUM
#PL
RewriteCond %{REQUEST_URI} ^/forum$ [NC]
RewriteRule ^(.*)$ forum/ [R=301,L]
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteCond %{REQUEST_URI} ^/forum_pl(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domena.pl/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_pl/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_pl$1 [L]
#UK
RewriteCond %{REQUEST_URI} ^/forum_uk(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_uk/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_uk$1 [L]
Final, working version:
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteRule ^forum_pl(.*)$ http://www.domena.pl/forum$1 [R=301,L] # Not working!
RewriteRule ^forum_uk(.*)$ http://www.domain.co.uk/forum$1 [R=301,L] # Not working!
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It is a pity that you can call from your browser forum_uk and forum_pl folders manually.
Not sure if I oriented correctly in your long question with some evolution :-)
Lets try this:
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum/(.*)$ http://www.domena.pl/forum_pl/$1 [R=301,L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum/(.*)$ http://www.domain.co.uk/forum_uk/$1 [R=301,L]
If you need the rewites to act differently, let me know
My client has a godaddy Virtual Dedicated Server (yes I know it sucks).... It has WHM VPS Optimized running on it .
I am trying to remove the index.php from the urls but my server still displays 404 errors I have looked at every forum in the universe and cant get an htaccess file that works. Here is what expression engine tells you to put use...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I know diddly about Apache.. please help kind sirs...
Test if the htaccess is working:
# Redirect domain
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$
RewriteRule ^/?$ "http\:\/\/google\.com" [R=301,L]
Or
# Remove the www from the URL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Ensure you actually have content appearing when you enter /index.php :P
Once you know your htaccess is working try "$1" instead of "%{REQUEST_URI}":
RewriteEngine on
RewriteCond $1 !^(img|admin\.php|themes|robots\.txt|index\.php|sitemap\.xml|/favicon\.ico) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
Also try "/index.php?/$1" instead of "/index.php/$1"
Ensure you have set EE (Admin->General Configuration) so "Name of your site's index page" is blank.
Remember that once you do get EE to work without the index.php appearing, it can be used both ways (with and without), which is bad for SEO, so remove it like so (place before the above rewrite :)
# Redirect index.php Requests (prevent site showing index.php in browser address bar)
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]