Hi there I'm working with .htaccess to set a couple of rules but my knowledge of .htaccess is rather limited. I've been searching only to find parts of what I need and where unable to make it work.
Currently I use the following rules to force all links to the index.php:
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
My issue is to combine this with the following rules:
1. Force http -> https
2. force www -> non www
3. Apply the top rules
As an example:
http://wwww.example.com -> https://example.com
I hope this makes sense; I've got quite some issues with the mod rewrite rules.
So thanks in advance ...
This must be added to your vritualhost config
<virtualhost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
This should be added to your virtualhost ssl config
<virtualhost *:443>
RewriteEngine on
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
This should work.
Related
I am using an AWS EC2 instance to host a website. I have an installation under /var/www/html/xyz/ dir & thus if I have to access website homepage, I have to specify link as www.example.come/xyz. I want that www.example.com should be redirected to www.example.com/xyz
Any idea how can I redirect www.example.com to www.example.com/xyz automatically (httpd config or anything similar)?
I checked online but most of the posts are related to subdomain redirects e.g. redirect admin.example.com to www.example.com which is not my issue.
Thanks!
my .htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your url is http://www.yourcompany.com/xyz
# So "RewriteBase" should be:
# RewriteBase /xyz
Options -MultiViews
RewriteBase /xyz
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
Another option is to change the document root of your site. If you are running Ubuntu, you should have a file in /etc/apache2/sites-available/YOUR_SITE.conf (maybe default.conf). Just edit it and change the DocumentRoot to point to your subfolder.
Create new .htaccess file with following code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /xyz/ [R=301]
you should have mod_rewrite enabled.
Use this code in your htaccess file, this will redirect the user to the subfolder without changing th URL www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
Another method with URL www.domain.com/subdir
RewriteEngine On
RewriteRule ^$ /subdir[L]
You can use the following code in Root/.htaccess:
RedirectMatch ^/$ /xyz
This will redirect
http://example.com
to
http://example.com/xyz
And you want to Rewrite http://example.com/foo to http://example.com/xyz/foo , you can use mod_rewrite :
RewriteEngine on
RewriteRule ^/?((?!xyz).*)$ /xyz/$1 [L]
Hope this will help !
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
or this
Redirect 301 /path/to-old-url http://www.cyourdomain.com/path/to-Try this new-url
I want to redirect all pages on my site (including index) to UnderWork.html
and I'm doing this using .htaccess with this code:
RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html
...and its works fine.
Now I am adding some more code to my .htaccess to redirect all traffic to the non-www domain and now my code looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html
Here is where I have a problem. If I enter a URL like: domain.in/xyz then it works as before, but if I use a URL like: www.domain.in/xyz then Apache converts it to coincart.inxyz/.
What am I doing wrong here? How can I get what i want? Thanks in advance.
Below are rules that work for me:
RewriteEngine On
# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]
# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]
# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]
To show a custom page when a directory root (domain root in particular) is requested, use the Apache's DirectoryIndex directive:
DirectoryIndex UnderWork.html
I am trying to create "pretty urls for dynamically created pages. My desire is to render all pages from the index page.
I am working on my computer using wamp 2.5/apache 2.4.9
The apache httpd.conf is set to:
Listen 0.0.0.0:7080
Listen [::0]:7080
DocumentRoot "c:/wamp/www/"
ServerName localhost:7080
I have tried several different approaches which have resulted in everything from a blank page returning to a 404 error page. With the code below it returns to the index.php in the /www/ folder in wamp.
This is my non-working .htaccess code:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
# Pages
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
# Rewrite to www.
RewriteCond %{HTTP_HOST} ^localhost:7080/demo [nc]
RewriteRule ^(.*)$ http://www.localhost:7080/demo/$1 [r=301,nc]
The clupret line of code is this I believe:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
which I am thinking is some sort of path issue but am not sure... any assistance would be appreciated.
The URL in the address is: http://localhost:7080/demo/test where demo is the site folder in the wamp/www/
http://localhost:7080/demo/test/ (with the forward slash) returns a 404 error.
Your htaccess is lacking a rewrite base.
Just after: RewriteEngine on
Add RewriteBase /demo/
It is strictly not allowing trailing slashes
Replace this line: RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
withthis code:
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]
This will allow urls ending trailing slashes to force back to your redirect url.
Remove this from the last line:
http://www.localhost:7080.
Leave it as just: /demo/$1 [r=301,nc]
The second to the last line is not doing a lot. You can do and I will advice you do away with it.
Please do remember that when leaving the localhost environment for an online host, replace all incidence of "/demo/" with just "/"
Your final htaccess should be someting like this:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
#RewriteCond %{REQUEST_FILENAME} !-d
# Pages
#RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]
# Rewrite to www.
RewriteRule ^(.*)$ /demo/$1 [r=301,nc]
I hope this helps
Try these rules in /demo/.htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
# Rewrite to www.
#RewriteCond %{HTTP_HOST} ^localhost$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}:7080%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?topic=$1 [L,QSA]
I've used the following rules in my htaccess file in other applications to redirect users from a folder to a subdomain but load the content from that folder when accessing that subdomain.
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
So if I go to: http://cameron.com/admin I end up on http://admin.cameron.com/
But the content is loaded from http://cameron.com/admin
However this doesn't work for CakePHP 2.x because of its rewriting apparently...
In my htaccess file in /app/webroot I have:
<IfModule mod_rewrite.c>
RewriteEngine On
# CAKEPHP RULES
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# REWRITE SUBDOMAIN TO FOLDER
RewriteCond %{HTTP_HOST} ^admin\.cameron\.com$
RewriteRule !^admin/? admin%{REQUEST_URI} [NC,L]
# REWRITE FOLDER TO SUBDOMAIN
RewriteCond %{THE_REQUEST} \s/admin/([^\s]*) [NC]
RewriteRule ^ http://admin.cameron.com/%1 [R=301,L]
</IfModule>
If I go to: http://cameron.com/admin it just tries to load the AdminController and doesn't redirect you, and if I go to: http://admin.cameron.com/ I just get a 500 Internal Server Error.
Any ideas on how to get this working for CakePHP?
CakePHP and almost any other PHP framework parse $_SERVER['REQUEST_URI'] in order to route your request to particular controller
more info here: https://github.com/cakephp/cakephp/blob/2.6/lib/Cake/Network/CakeRequest.php#L230 ,
so you simply need to have the SAME request URI parameters for your redirect to get app working.
For your old location it was "/admin", for new location it should be the same, but you do not want to pass it, so it is better to change the task like this:
"When you go to admin.site.com you will be redirected to site.com/admin".
This can be done simply like this:
RewriteRule ^([^.]+)\.example\.com http://example.com/$1
It is not a working example of rule because you also need to rewrite subdomain's request URI to to get everything working and it depends on your requirements, but can be smth like this:
RewriteRule ^([^.]+)\.example\.com(.*) http://example.com/$1/$2
UPD: real example
$ cat app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^admin.example.com
RewriteRule ^(.*)$ http://example.com/admin/$1 [P,L,NC]
</IfModule>
<IfModule mod_rewrite.c>
#standard cake htaccess stuff
...
RewriteCond %{HTTP_HOST} ^profile\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/users/profile/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^chat\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/chats/index/$1 [R=301,L]
</IfModule>
This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]