How to fix URL canonicalization with .htaccess? - .htaccess

I wan't the server to always redirect my URL's to format as "http://www.domain.com", even if the user write just "domain.com".
I could find some examples of this on the web, but I already have some fixes in .htaccess file and I don't know, where to put what, so it does't clash with the previous code.
Here's my .htaccess file:
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
What should I put in, so it does the URL redirect to "www"?
And one last question, is it all I have to do, so search engines don't have problems with the URL's?

I usually do the contrary.
you probably want www.example.com to forward to example.com -- shorter URLs
are sexier.
no-www.org/faq.php?q=class_b
Thanks to HTML5 boilerplate, I usually add :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If you want to do that in your .htaccess :
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
Else, try the inverse RewriteCond and associate RewriteRule

Your [^/\\.] pattern simplifies to [^/.] as the period does NOT need escaping.

Related

Rewrite rules - multiple requests priorities

I'm trying to make some sense with a multi-page and multi-domain redirect .htaccess and can't find a solution. Of course my bad...
For SEO optimization I need to redirect all the old subdomains pages of a project to the main domain root BUT some pages, that should be redirected to a chosen main domain subdirectories.
Example:
old.domain.com/ => www.domain.com/
old.domain.com/subdir => www.domain.com/
old.domain.com/specific => www.domain.com/newspecific
old.domain.com/another => www.domain.com/newanother
I'm not really good at rewriting rules and I came up with something like:
<IfModule mod_rewrite.c>
RewriteEngine On
# should be old.domain.com/specific => www.domain.com/newspecific
RewriteRule https://2014.designintown.org/2014/07/30/progetto-food-design/ https://www.designintown.org/progetto/ [R=301,L]
# should be old.domain.com/ => www.domain.com/
RewriteRule ^(.*)$ https://www.designintown.org/ [R=301,L]
</IfModule>
While the second rule seems to work, the first doesn't.
What am I getting wrong?
Thank you in advance for your support.
UPDATE
I eventually solved it with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/2014/07/30/progetto-food-design/$
RewriteRule ^(.*) https://www.designintown.org/progetto/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/borse-di-studio/$
RewriteRule ^(.*) https://www.designintown.org/agevolazioni/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/docenti/$
RewriteRule ^(.*) https://www.designintown.org/docenti/ [R=301,L]
RewriteRule ^(.*)$ https://www.designintown.org/ [R=301,L]
</IfModule>
I eventually solved it with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/2014/07/30/progetto-food-design/$
RewriteRule ^(.*) https://www.designintown.org/progetto/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/borse-di-studio/$
RewriteRule ^(.*) https://www.designintown.org/agevolazioni/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/docenti/$
RewriteRule ^(.*) https://www.designintown.org/docenti/ [R=301,L]
RewriteRule ^(.*)$ https://www.designintown.org/ [R=301,L]
</IfModule>

Htaccess issue with subfolder and with languange

I have a core PHP website that is hosted over a server and now I have moved the website to a subfolder to access the website something like that example.com/site. that website also use the .htaccess for managing multiple language.but after moving it to subfolder website is not accessible. Please see the following .htaccess file. This .htaccess is when the website is on root folder I want this .htacess to work when on subfolder /site/ please help me.
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) https://www.example.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
# URL rewriting module activation
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^en/([^?&]*)$ /$1?lang=en [QSA,L]
RewriteRule ^en/privacy-policy$ /privacy-policy.php?lang=en [QSA,L]
RewriteRule ^en/legal-notice$ /legal-notice.php?lang=fr [QSA,L]
RewriteRule ^fr/politique-de-confidentialite$ /privacy-policy.php?lang=fr [QSA,L]
RewriteRule ^fr/mentions-legales$ /legal-notice.php?lang=fr [QSA,L]
RewriteRule ^fr/([^?&]*)$ /$1?lang=fr [QSA,L]
</IfModule>
You may try these rules in /site/.htaccess:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^en/privacy-policy/?$ privacy-policy.php?lang=en [QSA,L]
RewriteRule ^en/legal-notice/?$ legal-notice.php?lang=fr [QSA,L]
RewriteRule ^fr/politique-de-confidentialite/?$ privacy-policy.php?lang=fr [QSA,L]
RewriteRule ^fr/mentions-legales/?$ legal-notice.php?lang=fr [QSA,L]
RewriteRule ^(en|fr)/(.*)$ $2?lang=$1 [QSA,L]
You can add
RewriteBase /site/
then write the conditions like
RewriteRule ^site/en/privacy-policy/?$ site/privacy-policy.php?lang=en [QSA,L]

How to hide page name and extention from url using .htaccess

I want to hide all my page names and extension from url,
htt://www.domain.com/innerpage.php
to
http://www.domain.com/
and
http://www.domain.com/subfolder/innerpage.php
to
http://www.domain.com/subfolder/
and
http://www.domain.com/subfolder/subfolder/innerpage.php
to
http://www.domain.com/subfolder/subfolder/
I used like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
its not work
I think this will work for you
DirectoryIndex innerpage.php index.php index.html index.htm
Put this as first line of your .htaccess. This directive will look for innerpage.php if no page has been specified for a directory. If you want to hide all pages in your site URL's then it's not a good idea IMO.
Give this set of directives a try:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/?$ /innerpage.php
RewriteRule ^([a-z0-9]+)/?$ /$1/innerpage.php
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /$1/$2/innerpage.php
RewriteRule ^innerpage.php$ / [R]
RewriteRule ^([a-z0-9]+)/innerpage.php$ /$1 [R]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/innerpage.php$ /$1/$2 [R]
And be surprise what will happened...

htaccess adding query string to 301 redirect

Can sombody help me out with this, im trying to re direct a page using htaccess file but it keeps adding ?c=oldpage on to the end of the new url, example:
http://www.mydomain.co.uk/newpage.html?c=oldpage
i have tried some of the solutions posted here but no luck, here is my .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteEngine on
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
Redirect 301 /oldpage.html http://www.mydomain.co.uk/newpage.html
ErrorDocument 404 /404.php
Thanks for any help.
This is mod_alias (the Redirect directive) and mod_rewrite not playing nicely with each other. Because both modules apply their directives on the same URI in the URL-file mapping pipeline, they don't know to ignore each other since neither directive knows what the other module is doing. Since you're targets overlap, both modules are applying their directives on the same URI and you get a mish-mashed result.
You need to stick with mod_rewrite in this case and move the redirect above the internal rewrites:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
# redirects
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^oldpage.html$ http://www.mydomain.co.uk/newpage.html [R=301,L]
# internal rewrites
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\/" [R=301,L]
RewriteOptions inherit
to a folder, or:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\.html$" [R=301,L]
RewriteOptions inherit
Don't know much about it, but it's what i use, hope it helps.

mod_rewrite config for /

I have the following .htaccess config
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule \.git - [F,L]
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
RewriteRule ^users/([0-9]+) profile.php?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?u=$1 [L]
Now, whenever somebody visits the landing page, they get redirected using the last rule for profile.php?u=$1.
How do I change the configuration so that www.example.com and www.example.com/ are mapped to index.php and not profile.php?
Match the empty string or single slash just after the ^index rule:
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
# Match root request with optional slash
RewriteRule ^/?$ index.php [L]
I will suggest not to do it this way.
Instead, simply user this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will send all your requests to index.php page, from there create a router.php and pass on the requests to that page, using php.
but in case you do, just add
RewriteRule ^/?$ index.php [L]
Like Michael suggested.
Here is a simple tool to test your rules, if you wish to
Apache RewriteRule tester

Resources