Category Archives: uri

htaccess rewrite for domains with non-www and https

Applies to domains using non-www and https (SLL).

After spending hours on stack overflow and hundreds of combinations, I finally managed to find a htaccess file that can do all I need.

This htaccess files can do the following:

  1. Redirect www to non-www
  2. Redirect http to https
  3. Preserve URI strings (the url part on your site)

    • http:// www. example .com --> https:// example .com
    • https:// www. example.com --> https:// example .com
    • http:// example .com --> https:// example .com
    • https:// example .com --> https:// example .com (will ignore)
    • http:// www. example .com/foo.html --> https:// example .com/ foo.html
    • https:// www. example .com/foo.html --> https:// example .com/ foo.html
    • http:// example .com/foo.html --> https:// example .com/ foo.html
    • https:// example .com/foo.html --> https:// example .com/ foo.html (will ignore)

Add the following code to your .htaccess file.

    RewriteEngine On
    RewriteCond   %{REQUEST_URI} !-U
    RewriteCond %{HTTPS_HOST} ^www\.example\.com$
    RewriteRule ^(.*)$ https://example.com/$1 [R=301]

Hope this helps someone! :-)

Rewrite language URI path to other path

I have a little trouble to implement a htaccess rewrite rule. I just need rewrite any language ID passed as URI to other URI inside my MVC framework.

Basically, when someone write something like:

http://www.example.com/en-us or http://www.example.com/pt-br

The URL to rewrite must be:

http://www.example.com/locale/to/en-us and http://www.example.com/locale/to/pt-br

I already try to implement something like that:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/(pt-br|en-us)
RewriteRule (.*) /locale/to/$1

But doesn't work, I can't get a rewrite using this code.

HTTP-like treatment for custom URI scheme, possible?

I defined a new URI scheme on my Windows system (following this thread: how do I create my own URL protocol? (e.g. so://...))

I want the custom URI protocol to act like HTTP within Chrome/Firefox...

That is, I want: myprotocol://localhost/test.html

to act exactly like: http://localhost/test.html

Is it possible, or does the browser insist on valid URI schemes, even if they are fully defined in the registry?

(This pertains to a local server and is required for personal application testing; I realise custom URI's are a bad standard and should not be used in production)

Display particular (dynamicaly) URL without parent folder. Leave The Others Alone

I started learning rewrites today

First of all here are examples of my links

Index.php?action=pictures
Index.php?action=pictures&type=kitchen
Index.php?action=pictures&type=ceiling
Index.php?action=services
Index.php?action=services&type=shower
Index.php?action=services&type=windows

In my .htaccess file I have this

RewriteEngine On
RewriteBase /
#Do not Rewrite files or folders
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]

#Ordinary
RewriteRule ^\.htaccess$ .htaccess [F]
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(.*)/?$ Index.php?action=$1 [L]

Rewrites URLs to

localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling

localhost/services
localhost/services/shower
localhost/services/windows

//etc

I like to have ALL my links working as is with an exception of services where I don't need parent folder /services/. Result:

localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling

localhost/services
localhost/shower
localhost/window

I tried to rewrite .htaccess but either I get only parentfolder to work or only subfolder. I tried to add this but I do understand that this matches everything...

RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]

I can however hardcode it like this

RewriteRule ^window/?$ Index.php?action=services&type=window [L]

Would like to have something dynamical. If folder services -> show no folder yet still be able to see localhost/services! Is it possible?

Java OData Service does not accept plus (+) signs in URI

I am trying to issue the following query using Apache Olingo for OData using Java:

URI customersUri = client.newURIBuilder(serviceRoot)
          .appendEntitySetSegment("Customers")
          .filter("CustomerID eq 'Joe'")
          .build();

The expected query string that I want is: $filter=CustomerID eq 'Joe'

However, when the library builds the above URI, the actual query string becomes like this:

%24filter%3DCustomerID+eq+%27Joe%27

Now, the problem is that when I use this query string for my OData Service, it seems that it does not accept the plus (+) signs. However, when removing the plus signs and use space instead, it works
Any help about this or recommendations please? Thanks

How to convert the file path to URI?

In phonegap, how to convert the file path into a URI? For an example, the filepath of the testwave.wav file is file:///storage/emulated/0/testwave.wav and the URI of it is content://com.android.providers.media.documents/document/audio%3A5821

What's the platform independent way of making this conversion? I'm asking this question because I noticed when I want to send the file as a binary data via a REST call, the URI is the one that actually worked.

Stream mp3 from protected directory in Android app

I have a problem with streaming mp3 file from protected directory on server. To protect directory I use simple .htaccess

AuthType Basic
AuthUserFile /home/domains/.htpasswd
require valid-user
AuthName "Protected directory"

To stream the mp3 I try to use sth like this:

        MediaPlayer mediaPlayer=new MediaPlayer();

        Context context=getContext();
        Uri myUri1 = Uri.parse("http:/user:[email protected]/uploads/ABC.mp3");

        try {
            mediaPlayer.setDataSource(context, myUri1);
        } catch (IllegalArgumentException e) {
            Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
            Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
            Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        }
        mediaPlayer.start();

Of course when I use this url in browser everything work fine. Any ideas how to fix this?