SEO friendly, non-www, hidden index.php URL - seo

I would like to do four things with apache mod_rewrite:
Remove www
http://www.example.com/ >>> http://example.com/
Remove index.php
http://example.com/index.php >>> http://example.com/
Rewrite URI
http://example.com/index.php?rewrite=request >>> http://example.com/request
Remove the last slash
http://example.com/request/ >>> http://example.com/request
I've read many examples but none of them worked properly.
Edit:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule ^([^/\.]+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/\.]+)$ index.php?rewrite=$1 [L]
</IfModule>
I using this right now, but it's not enough. Thanks

RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*)http://www.example.com/$1 [R=301,L]
RewriteRule ^([^/\.]+)/$ http:// %{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/\.]+)$ index.php?rewrite=$1 [L]
or
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\ HTTP
RewriteRule ^index\.php$ http:// www.example.com/ [R=301,L]
with apache mod_rewrite:

Related

Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS

I have a scenario for redirection I'm having trouble wrapping my head around.
The goals are:
old-domain.com/store needs to redirect to new-domain.com/store
There are three validation scripts that should remain on Old-Domain.com
Everything else from old-domain.com to redirect to new-domain.com
These rules need to apply to all scenarios HTTP, HTTPS and WWW (HTTP and HTTPS)
The problem I'm having is that the old-domain.com/store only properly redirects to new-domain.com/store when I remove the WWW redirects. I can't seem to get it to all play together nicely.
Here's what I've got:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ http://old-domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ https://old-domain.com/$1 [R=301]
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
Redirect 301 /store/ https://new-domain.com/store
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
We've also tried the following but then the validation scripts stop passing through:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://old-domain.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://old-domain.com/$1/ [R=301,L]
RewriteRule ^/?store https://new-domain.com/store [R=301,L]
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
The following rewrite rule will redirect all (www.)old-domain.com URLs to https://new-domain.com/ except URLs containing validation-script-(1|2|3).html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-(1|2|3)\.html$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/$1" [R=301,L]
Check this demo https://htaccess.madewithlove.be?share=f0deda97-dff8-569e-bd6f-411b8a779361
Edit:
to redirect old-domain.com/store or old-domain.com/store/ to new-domain.com/pages/store
RewriteRule ^store/?$ "https\:\/\/new-domain\.com\/pages\/store" [R=301,L]

Having a issue rewriting URLs via the htaccess file

I'm currently attempting to rewrite URLS for my employer's website for hopefully better results in SEO. However, I can't seem to get the url rewrite to work.
I have used this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myemployerssite.com.au$
RewriteRule (.*) https://www.myemployerssite.com.au/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 https://www.myemployerssite.com.au/page-not-found.php
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
</IfModule>
I expected, for example, the first service page which is civil construction to have it's URL to be rewritten from https://www.myemployerssite.com.au/services.php?serviceid=1 to https://www.myemployerssite.com.au/services/civil-construction
But the URL remains as https://www.myemployerssite.com.au/services.php?serviceid=1
The redirect from non-www to www is working as intended as is the redirect from to http to https.
Some advice would be appreciated
I changed the order of the rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
RewriteCond %{HTTP_HOST} ^myemployerssite.com.au$
RewriteRule (.*) https://www.myemployerssite.com.au/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
ErrorDocument 404 /page-not-found.php
Try changing this line
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
to
RewriteCond %{QUERY_STRING} serviceid=1$
RewriteRule ^services.php$ /services/civil-construction [L,R]

Redirect apache www, https://www to https non-www URLS

I know it's duplicated many times but I promise I tried many different way but still didn't work for me.
I have a https://example.com and it redirect from http and www but it's not covering all URLs.
I want to redirect
www.example.com to https://example.com covered
https://www.example.com to https://example.com covered
https://www.example.com/about to https://example.com/about
https://www.example.com/#blah to https://example.com/#blah
This is my last time tried .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
</IfModule>
----------------------Updated------------------
I have no idea how it's worked. I replaced many different rules but this one is only one working.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
Change yours rules to these:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>

How to force https and www on symfony 4

I have a problem that I don't understand with symfony. I would like to force https and www on my .htaccess, but I don't understand anything with symfony.
Here is my .htaccess inside www file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]
And in my public file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
I don't know what I need to write and where ...
Include in your .htaccess file:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The first two lines are for redirecting to https, the second two lines for www.
try this in your .htaccess file :
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

htaccess rewrite for only root domain, selected files and selected folders

Been fidling with it my self but I think I am doing it all wrong, please advice:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\-f
RewriteRule ^folder1(.*)$ https://newdomain.com$1 [L,R=301]
RewriteRule ^folder2(.*)$ https://newdomain.com/$1 [L,R=301]
RewriteRule ^folder3(.*)$ https://newdomain.com/somefolder/$1 [L,R=301]
RewriteRule ^page1\.html https://newdomain.com/$1 [L,R=301]
RewriteRule ^page2\.html https://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^/?$ https://newdomain.com/ [L,R=301]
</IfModule>
If I request a page/folder for olddomain.com which is not in .htaccess it should not rewrite.
Does anyone have more skills in .htaccess than me?
Thanks,
Marc
Probably you should prepend an additional condition among the ones about newdomain
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com$ [NC]