Rewrite URL to different directory and change extension - .htaccess

I've got a problem with some rewrites that I have checked extensively in:
https://htaccess.madewithlove.be/
But on the web server don't work as expected.
My rules are:
ReWriteEngine On
RewriteRule ^$ /content/index.php [L]
RewriteRule ^(.*)/(.*).html$ /content/$1/$2.php [L]
RewriteRule ^(.*)/$ /content/$1/index.php [L]
RewriteRule ^(.*)$ /content/$1/index.php [L]
Example URLs are:
https://domain.tld -> /content/index.php
https://domain.tld/ -> /content/index.php
https://domain.tld/path -> /content/path/index.php
https://domain.tld/path/ -> /content/path/index.php
https://domain.tld/path/page.html -> /content/path/page.php
https://domain.tld/path/more -> /content/path/more/index.php
https://domain.tld/path/more/ -> /content/path/more/index.php
https://domain.tld/path/more/page.html -> /content/path/more/page.php
I'm getting an internal server error, even though the pages don't have anything on them. If I remove the routing I get the 404 fine.
Any help appreciated. If you need more info just ask.

Check this rules on the top of your .htaccess file
RewriteEngine on
RewriteBase /content/
# path/more/page.html -> /content/path/more/page.php
RewriteRule ^(.*)\/(.*)\/(.*)\.html$ /$1/$2/$3.php [L]
# path/more/ -> /content/path/more/index.php
RewriteRule ^(.*)\/(.*)\/$ /$1/$2/index.php [L]
# path/page.html -> /content/path/page.php
RewriteRule ^(.*)\/(.*)\.html$ /$1/$2.php [L]
# path/more -> /content/path/more/index.php
RewriteRule ^(.*)\/(.*)$ /$1/$2/index.php [L]
# path/ -> /content/path/index.php
RewriteRule ^(.*)\/$ /$1/index.php [L]
# path -> /content/path/index.php
RewriteRule ^(.*)$ /$1/index.php [L]
# / -> /content/index.php
RewriteRule ^\/$ /index.php [L]
# https://domain.tld -> /content/index.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld [NC]
RewriteRule ^ /index.php [L]

Related

301 permanent redirection for various depth

I use this code for R301 for 1 depth of category.For example I want redirect http://sitesample.ltd/new/mynews to http://sitesample.ltd/new/this is my code:
RewriteRule ^mynews/(.+)$ http://sitesample.ltd/new/$1 [R=301,L]
And I want remove depth 2 and 3 from url. For example redirect http://sitesample.ltd/new/mynews/photo to http://sitesample.ltd/new/.I try this but not work:
RewriteRule ^mynews/(.+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/(.+)/?$ http://sitesample.ltd/new/$3 [R=301,L]
Another try but no luck:
RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/([^/]+)/?$ http://sitesample.ltd/new/$3 [R=301,L]
If you try to do that:
mynews/xxxx -> http://sitesample.ltd/new/xxxx
mynews/yyyy/xxxx -> http://sitesample.ltd/new/xxxx
mynews/zzzz/yyyy/xxxx -> http://sitesample.ltd/new/xxxx
You can do that:
RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
Or only this:
RewriteRule ^mynews/(?:[^/]+/){0,2}([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

using multiple rewrites in .htaccess to remove duplicate content

So basically I want everyone who asks example.com/index.php to get 301 redirected to example.com/,
everyone who asks for any www. urls to get 301 redirected to corresponding non-www site
(www.examle.com/foo -> example.com/foo)
and everyone who asks for site with double [also triple etc.] slashes in url to get 301
redirected to url with double slashes remowed (example.com////foo -> example.com).
And if anyone should do any combination of these three cases, he should still get 301 redirected to right url (www.example.com////index.php -> example.com).
So I come up with that:
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,N]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*)$ /$1 [R=301]
BUT, www.example.com//foo -> http://example.com/http:/example.com/foo = 404!
How to get it working?
Try these lines :
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]
RewriteRule ^index.php$ / [L,QSA,R=301]
RewriteRule ^(.*)/{2,}(.*)$ /$1/$2 [L,N,QSA,R=301]
Screw logic, seriously...
I found out, that folloving stuff in .htaccess does proper (almoust, with little quirks) 301 redirects to right places...
RewriteBase /
RewriteEngine on
...at the top, and the rewrite rules themselves:
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]
RewriteRule ^index\.php$ / [QSA,R=301]
I swear that these are only rewrite rules in my .htaccess.
The quirk is, that www.example.com//foo redirects(301) to example.com/foo, and then example.com/foo redirects(301) again to example.com/foo, bot only once... (no loops)
www.example.com//foo -> example.com/foo -> example.com/foo
Also when www and any other condition is met, redirection happens in steps:
www.example.com/index.php -> example.com/index.php -> example.com/
www.example.com//index.php -> example.com/index.php -> example.com/
But that's not really big problem...

multiple redirects with .htaccess

I want to do this
website.com/blog/index.php -> website.com/blog
website.com/admin/archief_login.php -> website.com/admin
this works with my code.
but I want to add this:
website.com/aa -> website.com/web/index.php?init=aa
for some reason the blog gets this redirect: website.com/blog/?init=blog
what is the best way to set these different rewrites?
RewriteEngine on Options All -Indexes
RewriteCond %{HTTP_HOST}
^websit.com$ [OR] RewriteCond
%{HTTP_HOST} ^www.website.com$
RewriteRule ^admin$
"http\:\/\/www.website.com\/admin/archief_login.php"
[R=301,L]
RewriteRule ^blog$
"http\:\/\/www.website.com\/blog/index.php"
[R=301,L]
DirectoryIndex client_login.php
RewriteRule
^screen-([a-zA-Z0-9_-]+).html$
index_client.php?screen=$1
RewriteRule
^invoice([a-zA-Z0-9_-]+).html$
make_invoice.php?id=$1
RewriteRule
^pack-([a-zA-Z0-9_-]+).html$
index_client.php?screen=pack_code&wwwcode=$1
You need to put the more "general" rules lower in the file so they don't match almost all of your URLs
RewriteRule ^(\w)$ /web/index.php?init=$1 [L, NC]
RewriteRule ^blog$ /blog/index.php [R=301,L]
The above will do
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/web/index.php?init=blog
If you reverse the two rules you will get
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/blog/index.php

htaccess rewrite only part of the querystring

I am trying to perform the following rule in htaccess:
www.domain.com/folder/?id=14077&c=en-gb -> www.domain.com/folder/?id=14077
www.domain.com/folder/?c=en-gb&ID=14077 -> www.domain.com/folder/?id=14077
www.domain.com/folder/?id=14077&c=fr-fr -> www.domain.fr/folder/?id=14077
www.domain.com/folder2/?c=fr-fr&ID=14077 -> www.domain.fr/folder2/?id=14077
www.domain.com/folder2/?c=en-us&ID=14077 -> www.domain.us/folder2/?id=14077
Basically take out the "c" part of the querystring and redirect it to a new domain, based on the following rules:
c=en-gb -> www.domain.com
c=fr-fr -> www.domain.fr
c=en-us -> www.domain.us
Any help welcome!
Admittedly, I'm not sure if this is a task best solved by mod_rewrite...but what the hell, why not:
(Not fully tested, but it seems to work well)
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)(\A|&)c=([A-Za-z\-]+)&?(&.*)?$
RewriteRule .* - [E=SWITCHLANG:%3,E=QSONE:%1,E=QSTWO:%4]
RewriteCond %{ENV:SWITCHLANG} =en-gb [NC]
RewriteCond %{HTTP_HOST} !(.*)\.com$
RewriteRule (.*) http://www.domain.com/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =fr-fr [NC]
RewriteCond %{HTTP_HOST} !(.*)\.fr$
RewriteRule (.*) http://www.domain.fr/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =en-us [NC]
RewriteCond %{HTTP_HOST} !(.*)\.us$
RewriteRule (.*) http://www.domain.us/$1?%{ENV:QSONE}&%{ENV:QSTWO}

Resources