Difficulty with rewriting friendly Urls

Asked

Viewed 507 times

1

I don’t have much experience with . htaccess and am having some difficulty rewriting a website’s Urls.

On the site in question, I first forced the rewriting 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 redirected 301 dominio.com.br for www.dominio.com.br successfully. The rule also applies to index or index.php after the base URL (eg: www.dominio.com.br/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 friendly. They follow the pattern below:
www.dominio.com.br/index.php? link=about

As the rewrite removes the index, they look like this:
www.dominio.com.br/? link=about

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

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

This was one of the rules I tried, including testing in Sublime/netbeans, using the regex of them working out there. It turns out that when you test on the server, no rewriting occurs. I tested several other rules by reading the documentation, such as [NC],[L] among others. Some result in 500 error.

So my question is whether the use of Https, the deletion of the index or the server itself may be affecting these rewrites?

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

Example 1
Example 2

2 answers

1

Basically you’ll do the HTACCESS send to PHP to route the URL. No need to do querystring as 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]


ROUTE

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 not to conflict with values of $_GET.

These ways to route the URL is simple and keeps the logic in PHP, all information from your URL will be in the array router, you decide how to check the $router[X].

  • i can perform this treatment in a config.php file, for example, or through a function?

  • Can give a normal function: $rotas = router(), I’ll add the example

0

In relation to HTTPS, it is not recommended to make a 301 redirect for this, due to SEO.

For this just enable HSTS in Apache2. With HSTS enabled, all user navigation on your domain/subdomain will be performed via HTTPS.

Obs.: This method is used by google, twitter, facebook, etc. Reference: https://www.owasp.org/index.php/HTTP_Strict_Transport_Security

As for the route issue is as Papa Charlie recommended, you will pass the REQUEST_URI to PHP and within the application you will make the routes.

Cakephp is a framework that works this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.