Redirecting URL without www to www - mod-rewrite

I need your help. I want to test if the URL is entered without www
like example.com it should be forwarded to www.example.com.

Try this mod_rewrite rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [NC,L,R=301]

If you are using nginx, then add this line to nginx config:
server {
listen 80;
server_name yourdomain.com;
rewrite ^/(.*) http://www.yourdomain.com/$1 permanent;
}

Related

laravel when page has / in url it redirect public/

I have an issue that if you write / after then URL it changes to public/
Example:
mysite.com/url (is working just fine)
mysite.com/url/ redirects to mysite.com/public/url
I have following .htaccess file which works for removing www from URL, redirects to https and show projekt from public folder. All these I still need. But also need fix the / issue. Any suggestions ?
# 301 redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# SHOW FILES FROM PUBLIC FOLDER
RewriteRule ^(.*)$ public/$1 [L]
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Check this file: app\Providers\RouteServiceProvider.php

URL rewriting QUERY_STRING?

I've tried and looked for lot of your posts,
cannot get it working yet.
all URL from the site I have a simple PHP config like this example :
http://www.example.com/index.php?page=pagename
I'm wondering if I can hide Quesry_String like this :
http://www.example.com/pagename/
In the server config (apache 2.2 using virtual host without .htaccess) :
DocumentRoot "/home/account/www/www.mysite.com/"
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
## Rewrite 301 old Quesry_string (?p => ?page) ##
RewriteCond %{QUERY_STRING} ^p=([^&]+) [NC]
RewriteRule ^/$ /?page=%1 [R=301,L]
## Trying to hide query_string (not working) ##
#RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
#RewriteRule ^/$ /%1 [R=301,L]
#result => http://www.www.mysite.com/pagename?page=pagename (404)
## Rewrite 301 old page names ##
RewriteCond %{QUERY_STRING} ^page=oldpagename$ [NC]
RewriteRule ^/index\.php$ /?page=pagename/ [R=301,L]
## Remove index.php from url ##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
#Rewrite 301 index.html to root /
RewriteRule ^/index.htm?$ index.html [NC,R=301,L]
RewriteRule ^/index.html?$ / [NC,R=301,L]
I guess I'm almost there, thank you for your help.
It should work for you i guess
RewriteRule ^([^/.]+)/$ index.php?page=$1 [L]

Redirect subdomain, but only if site root requested

I can't seem to find an answer to this specific question:
How can I forward http://play.domain.com -- but NOT http://play.domain.com/xxx
To:
http://work.domain.com
Where "xxx" is any string.
In other words, if the user requests the site root, redirect, otherwise stay on the chosen URL.
I've tried
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^(.*)$ http://work.domain.com.com/$1 [R=301,L]
But it's not doing anything :-/
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play\.domain\.com$ [NC]
RewriteRule ^/?$ http://work.domain.com/ [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^$ http://work.domain.com.com/ [R=301,L]
to only redirect the blank request.

mod_rewrite to force SSL for a subfolder

I try to make a redirect from a special folder to a special host.
When somebody enter http://mydomain.com/administrator he should be redirected to https://a-otherdomain.com/mydomain.com/administrator
What I have is this in a .htaccess file in DOCUMENT_ROOT .. but it want work :(
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/administrator(/*) [NC]
RewriteRule ^/administrator/(.*)$ https://a-otherdomain.com/mydomain.com/administrator/$1 [QSA,R=301,L]
Any Ideas?
Update:
now I use this, which works in apache configuration files for the vhost:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/administrator/ [NC]
RewriteRule ^/(.*)$ https://a-otherdomain.com/mydomain.com/$1 [R=301,L]
Thanks,
Thomas
RewriteRule doesn't match leading slash in a URI.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteRule ^administrator(/.*|)$ https://a-otherdomain.com/mydomain.com/$0 [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

mod_rewrite .htaccess HTTPS

I would like to force the browser to redirect to HTTPS always if someone types in:
http://www.mydomain.com/user
or
http://www.mydomain.com/user/
Both of the above 2 links should redirect to: https://www.mydomain.com/user
Right now the 2nd to bottom line of code works correctly:
RewriteRule ^user$ https://www.mydomain.com/user/ [R,QSA]
will indeed redirect the browser to HTTPS but if http://www.mydomain.com/user/ is input then it won't redirect to HTTPS. How can I achieve this?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteRule ^user$ https://www.mydomain.com/user/ [R,QSA]
RewriteRule ^user/$ public/index.php?var1=x&var2=y [L,QSA]
Use these rules:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# force www domain name
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
# force HTTPS for some pages
RewriteCond %{HTTPS} =off [NC]
RewriteRule ^user/?$ https://www.mydomain.com/user/ [R=301,L]
RewriteRule ^user/$ public/index.php?var1=x&var2=y [L,QSA]
It will redirect to HTTPS:
if HTTP is current protocol (so no redirection if already on HTTPS)
regardless of trailing slash presence: both /user and /user/ will do the job (but will do nothing if /user/something-here will be requested).
How about this ...
RewriteRule ^user/(.+)/?$ public/index.php?var1=x&var2=y [L,R=301]
RewriteRule ^user/?$ https://www.mydomain.com/user/ [L,QSA]
RewriteRule ^(.*)/?$ http://www.mydomain.com/$1 [L,R=301]

Resources