Difficulty with rewriting friendly URLs

1

I'm not very experienced with .htaccess and I'm having some difficulty rewriting the URLs of a site.

In the site in question, I first forced the rewrite of the URL to HTTPS (the site did not have SSL before):

Options +FollowSymlinks

ErrorDocument 404 https://www.dominio.com.br/pagina404.html
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Then I successfully redirected domain.com to www.domain.com . The rule also applies to index or index.php after the base URL (eg www.domain.com/index.php).

RewriteCond %{HTTP_HOST} ^dominio\.com\.br
RewriteRule ^(.*) https://www.dominio.com.br/$1 [R=301,L]
RewriteRule ^index(/|.php)?$ https://www.dominio.com.br [R=301,L]

So far, everything worked. It turns out that the URLs on this site are not user friendly. They follow the pattern below:
www.dominio.com.br/index.php?link=sobre

As the rewrite removes the index, it looks like this: www.dominio.com.br/?link=sobre

So I force the rewrite using the snippet below, to try a URL like this: www.domain.com / over

RewriteRule ^\?link=(.*) /$1
RewriteRule ^/(.*)(.php)?\?link=(.*) /$3

This was one of the rules I tried, including testing on Sublime / netBeans, using their regex module, working right there. It happens that when I test on the server, no rewrite occurs. I have tested several other rules by reading the documentation, like [NC], [L] and others. Some result in a 500 error.

So my question is whether using HTTPs, deleting the index, or the server itself might be affecting these rewrites?

Note: I tried some of the options shown in other topics, like these:

Example 1
Example 2

    
asked by anonymous 27.09.2014 / 07:03

2 answers

1

You will basically have the HTACCESS command for PHP to route the URL. It is not necessary to do as querystring because PHP will use REQUEST_URI to route the request.

.HTACCESS

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [QSA,L]


ROTA

function router(){
    // www.domain.com/empresa/sobre
    $url = ltrim( parse_url( $_SERVER['REQUEST_URI'] , PHP_URL_PATH ) , '/' );
    return explode( '/' , $url );
}


USE

// array(0 => 'empresa' , 1 => 'sobre' )
$router = router();

I prefer to route based on REQUEST_URI so it does not collide values from $ _GET .

This way to route the URL is simple and maintain logic in PHP, all your URL information will be in the router array, you decide how to check the $router[X] indexes.

    
27.09.2014 / 08:12
0

Regarding HTTPS, it is not recommended to perform a 301 redirect for this, due to SEO.

To do this, simply enable HSTS in Apache2. With HSTS enabled, all user navigation in your domain / subdomain will be done via HTTPS.

Note: This method is used by google, twitter, facebook, etc. Reference: link

As for the route question is how Papa Charlie recommended, you will pass the REQUEST_URI to PHP and within the application you are going to do the routes.

CakePHP is a framework that works this way.

    
24.05.2015 / 03:11