.htaccess redirection with multiple subdomain/folder - apache

I have three folders : the base (root like www) and 2 folders :pro / perso.
I have 2 subdomains : pro.mysite.com and perso.mysite.com
I want only one .htaccess, on root and i want to create redirection for both subdomains/folders.
Like if i write mysite.com/Pro/index.php?p=cake i want to be redirected on pro.mysite.com/cake
mysite.com/Pro to pro.mysite.com is OKAY by the hosting tools.
For the pages, i have big issues. Here's my htaccess :
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule /?Pro/cake /Pro/index.php?p=cake [L]
RewriteRule /?Perso/kitten /Perso/index.php?p=kitten[L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L,R=301]
Actually its work but... Without knowing why, it removes my "index.php", if i go on : perso.mysite.com/kitten im redirected on : perso.mysite.com/?p=kitten
Here's my actual.htaccess :
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteRule /?Pro/revue-presse-articles-sport-coach-alsace /Pro/index.php?p=presse [L]
RewriteRule /?Pro/cachier-des-charges-sport-entreprise-coaching /Pro/index.php?p=cahierCharges [L]
RewriteRule /?Pro/offre-coaching-sport-entreprise-alsace /Pro/index.php?p=offres [L]
RewriteRule /?Pro/contact-entreprise-pro-coaching-sportif-coach-sport-alsace /Pro/index.php?p=contact [L]
RewriteRule /?Pro/temoignages-coaching-entreprise-coach-societe-sport-alsace /Pro/index.php?p=temoignages [L]
RewriteRule /?Pro/activites-sportives-entreprise-pro-coaching-sportif /Pro/index.php?p=activites [L]
RewriteRule /?Pro/coaching-sport-entreprise-pro-coach-sportif-alsace /Pro/index.php?p=presentation [L]
RewriteRule /?Particulier/coaching-activites-sportives-particuliers /Particulier/index.php?p=activites [L]
RewriteRule /?Particulier/concept-coach-coaching-individuel-collectif-domicile /Particulier/index.php?p=concept [L]
RewriteRule /?Particulier/studio-sport-sante-alsace-apas /Particulier/index.php?p=studioSante [L]
RewriteRule /?Particulier/equipe-staff-membres /Particulier/index.php?p=equipe [L]
RewriteRule /?Particulier/cachier-des-charges-sport-particuliers-domicile-coaching /Particulier/index.php?p=cahierCharges [L]
RewriteRule /?Particulier/coaching-sportif-domicile-alsace-bas-rhin-haut-rhin-strasbourg /Particulier/index.php?p=couvertureGeo [L]
RewriteRule /?Particulier/offre-coaching-sport-domicile-alsace /Particulier/index.php?p=offre [L]
RewriteRule /?Particulier/tarifs-prix-offres-coaching-sportifs-sport-alsace /Particulier/index.php?p=tarifs [L]
RewriteRule /?Particulier/contact-coaching-sportif-domicile-coach-sport-alsace /Particulier/index.php?p=contact [L]
RewriteRule /?Particulier/temoignages-coaching-domicile-coach-sport-alsace /Particulier/index.php?p=temoignages [L]
RewriteRule /?Particulier/coaching-sport-domicile-coach-sportif-alsace /Particulier/index.php?p=presentation [L]
RewriteRule /?Particulier/nos-coachs-sportifs-domicile-coach-alsace /Particulier/index.php?p=coachs [L]
ErrorDocument 404 https://www.example.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L,R=301]
So, if we take the first one, actually if i go on : pro.example.com/revue-presse-articles-sport-coach-alsace it redirect me on pro.example.com/?p=presse
My only wish is to be redirected on :
pro.example.com/revue-presse-articles-sport-coach-alsace (without seeing the true page index.php?blabla etc)
or pro.example.com/index.php?p=presse

Related

How can I redirect all www traffic to non www url in vue project

I have the following code in my htaccess file which is supposed to redirect all www requests to the non www version of index.html, but its not working and I'm not sure why. Can anyone help?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Have it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Here we are capturing value after www. in a capture group and then using that as %1 in target URL.
Make sure to clear your browser cache or use a new browser before you test this change.

Priority redirecting using .htaccess

I want to set a priority in my .htaccess.
Ok, I've got folders on my root project, schema:
-root:
-core
-includes
-pages:
- profile.php
-etc
-support
-infractions
-.htaccess
and what I want to do is:
If the URL is like https://example.com/support, show the /support/index.php.
I've tried -Indexes option but it doesn't works or doesn't meet my needs.
My .htaccess is like:
Options +FollowSymlinks
Options +Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^fenixcms\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://fenixcms.com/$1 [R,L]
RewriteBase /
DirectoryIndex pages/index.php
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^alert/([^/\.]+)/?$ pages/alert.php?id=$1 [L]
RewriteRule ^player/([^/\.]+)/?$ pages/profile.php?name=$1 [L]
RewriteRule ^forums/new_topic/([^/\.]+)/?$ /forums/new_topic.php?c=$1 [L]
RewriteRule ^forums/topics/([^/\.]+)/posts/edit/([^/\.]+)/?$ /forums/edit_post.php?tid=$1&pid=$2 [L]
RewriteRule ^forums/topics/([^/\.]+)/posts/new/?$ /forums/new_post.php?tid=$1 [L]
RewriteRule ^forums/topics/([^/\.]+)/posts/reply_to_id/([^/\.]+)/?$ /forums/new_quote.php?tid=$1&pid=$2 [L]
RewriteRule ^forums/topics/([^/\.]+)/edit/?$ /forums/edit_topic.php?tid=$1 [L]
RewriteRule ^forums/topics/([^/\.]+)/?$ /forums/topic.php?id=$1 [L]
RewriteRule ^forums/topics/([^/\.]+)/posts/delete/([^/\.]+)/?$ /forums/delete_post.php?tid=$1&pid=$2 [L]
RewriteRule ^forums/categories/([^/\.]+)/?$ /forums/categories.php?s=$1 [L]
RewriteRule ^forums/posts/([^/\.]+)/?$ /forums/posts.php?pid=$1 [L]
RewriteRule ^forums/([^/\.]+)/?$ /forums/$1.php [L]
RewriteRule ^revisions/([^/\.]+)/?$ pages/revisions.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/([^/\.]+)/?$ /$1/$2.php [L]
RewriteRule ^([^\.]+)/?$ /$1/index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ pages/$1.php [NC,L]

htaccess rewriterule adds unwanted get parameter

What I have in my htaccess file:
RewriteEngine on
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]
What I like it to do is redirect example.com/city/london to example.com/uk/city/london
The weird thing is that it now redirects to example.com/uk/city/london?city/london so it seems it adds the part it needs to redirect as a get parameter to the new URL.
Also tried Redirect 301 /city/london http://www.example.com/uk/city/london but this gives the same result.
You need to keep R=301 (redirect) rule before internal routing rule:
RewriteEngine on
RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Also clear your browser cache to test this to avoid hitting old 301 cache.
You can try something like this if the Redirect 301 did not work:
RewriteRule /city/london uk/city/london [L]
Here's an example with your other defined .htaccess rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule /city/london uk/city/london [L]
# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]

Redirecting HTTP to HTTPS correctly in Question2Answer script

I have searched for this question for many days and tried so many different methods but nothing works so far. I am using the Question2Answer script and I want to redirect all the HTTP requests to HTTPS.
My URL structure is set to :
/123/why-do-birds-sing (requires htaccess file)
and the htaccess file is as follow:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
This correctly redirects http://example.com to https://example.com. However, if the user enters an address like this : www.example.com/users they are redirected to https://example.com/index.php?qa-rewrite=users which returns a 404 error.
The index.php?qa-rewrite= is added automatically and removing it from the htaccess totally messes up everything and I think it should be there.
This is because you need all of your redirect rules before any of the routing rules (the ones without the R flag), so:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
I use this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
If you don't like to redirect www, you can remove the line
RewriteCond %{HTTP_HOST} ^www\.example\.com$ and the [OR] above

.htaccess redirect trouble

I'm having trouble with (what should be) a simple .htaccess redirect. I need to changed any url that has "for_homes" into "for_home".
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
However when I go to any pages with for_homes I get a 404.
Rewrite Rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
</IfModule>
The problem is a preceeding rule is rewriting to index.php. In this block here:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### This rule rewrites "for_homes" to "index.php?/for_homes"
RewriteRule ^(.*)$ index.php?/$1 [L]
### These rules never get applied
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
So perhaps after the RewriteCond %{REQUEST_FILENAME} !-d, add conditions to exclude the later 2 rewrites, So that it looks like this:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/for_home(s)?
RewriteCond %{REQUEST_URI} !^/for_business(es)?
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
If it's your intention to have index.php handle for_home and for_businesses, so that when someone enters http://domain.com/for_homes/stuff in their browser's address bar, the browser gets redirected to http://domain.com/for_home/stuff, then it gets internally rewritten to /index.php?/for_home/stuff so that index.php can handle the request, you simply need to move the 301 redirects before the index.php rewrite:
### Redirect first
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Perhaps you are just missing RewriteEngine On
RewriteEngine On
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]