I was wondering if there is anyway to do a mod-rewrite so if the user types http://www.url.co.uk, it will automatically re-direct them to https://www.url.co.uk/secure/folder/
? If not, I can move everything into the root, and then do a PHP re-direct if there's no https, but that's not ideal for this case...
Thanks!
Sure it can be done using mod_rewrite. Place this code in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^$ https://%{HTTP_HOST}/secure/folder/ [L,R]
That was answered here:
HTTP to HTTPS using mod_rewrite
You can easily add your custom folder in the statement on the third line.
Related
I am looking for a .htaccess code to redirect HTTPS requests on a certain url/page to HTTP (without SSL, yes the otherway around).
Please help.
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^surf\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=301]
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
My site www.example.com has its document root to public_html directory. But, all of my content is hosted in public_html/www.example.com/. So, I want when a user visits my site he get contents from public_html/www.example.com/ and not public_html. So, what should be the .htaccess rewrite rule for that?
I am not good in rewrite rules but the best I found was:
RewriteRule !^blog blog%{REQUEST_URI}
There are two problems with this.
It will not work properly if user requested www.example.com/blog/
It will also not work when we use directory name as www.example.com
Give this a try and let me know if it works for you, and if it doesn't try to describe as detailed as possible what happens.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/www\.example\.com [NC]
RewriteRule ^(.*)$ /www.example.com/$1 [NC,L]
I have a page with the URL of http://www.freshmarketstores.com/departments/produce/recipe.php?ing=Akane_Apples&recipe=59 that I would like to be cleaned up to look like http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59
Right now, in my .htaccess file that is located in the produce folder, I have the following code:
RewriteEngine on
RewriteRule ^recipe/(.*)/(.*)$ recipe.php?ing=$1&recipe=$2 [L]
Currently, this does not affect anything. when the recipe.php?ing=Akane_Apples$recipe=59 url is loaded, it loads fine, but does not do the rewrite. This is my first attempt at URL rewriting and can't figure this thing out.
Thanks in advance for your help.
EDIT:
Okay, so after some learning last night, I realize I was approaching this backwards. I am able to type in http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59 and get the resource that is stored at .../produce/recipe.php?ing=Akane_Apples&recipe=59. Thanks for all of your help on that.
My question now, is can you go the opposite way? If I type in .../produce/recipe.php?ing=Akane_Apples&recipe=59, how can I get it to redirect to the /produce/recipe/Akane_Apples/59?
Ensure you have the
AccessFileName .htaccess
setup in your Server and virtual host configuration files in apache, the default is setup to be .htaccess however it is not restricted to only that and can be named anything.
default config file for a debian based system is /etc/apache2/apache2.conf and for most other systems httpd.conf in the apache install directory
once the access file is setup and you have mod_rewrite enabled you can do something as below
http://www.freshmarketstores.com/departments/produce/Akane_Apples/59
RewriteEngine On
RewriteRule ^recipe/([^/]*)/([^/]*)$ /recipe.php?ing=$1&recipe=$2 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# internal forwarding to /recipe.php
RewriteRule ^recipe/([^/]+)/([^/]+)/?$ recipe.php?ing=$1&recipe=$2 [L,QSA,NC]
# external redirect to /recipe
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+recipe\.php\?ing=([^&]+)&recipe=([^\s]+) [NC]
RewriteRule ^ recipe/%1/%2? [R=301,L]
You should be okay with
RewriteEngine on
RewriteCond %{THE_REQUEST} \?ing=([^&]+)&recipe=([^&]+) [NC]
RewriteRule ^(departments/produce/recipe)\.php$ http://%{HTTP_HOST}/$1/%1/%2 [NC,R=301,L]
I own two domains example1.com and example2.com. I setup example1.com to point to the the "/" of my Django project, meaning that example1.com/* is handled by urls.py. I want example2.com to actually point to a specific Django URL (e.g. "/123"), which is currently accessed at example1.com/123.
Ideally, when you go to example2.com/abc, Django would treat the request as if it came from example1.com/123/abc, but the URL would still be "example2.com/abc".
Here's what I have currently in my .htaccess file, but example2.com leads to "Server not found" in Firefox.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule ^\.com(.*)$ http://example1.com/123/$1 [L]
This is my first experience doing something like with the htaccess file, so apologies if something is off. I am using mod_wsgi (on WebFaction)--does this still support rewriting URLs? Thank you for your help.
Try adding the following to your htaccess file in the root directory of your example2.com site.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
#proxy all requests to example1.com/123
RewriteRule (.*) http://example1.com/123/$1 [P]
I needed to edit httpd.conf, as opposed to .htaccess. Here are the relevant lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?example2.com$ [NC]
RewriteRule ^(.*)$ http://example1.com/123/$1 [P,L]
These lines go directly beneath the WSGIScriptAlias that directs to the Django project .wsgi file. I don't know why the .htaccess file did not do the trick, but hopefully this helps out others with the same problem.
Thanks!