htaccess – Need help writing the correct rule

I’m using SimpleMVCFramework

All my routes are working fine, based on the default htaccess file:

Options -Indexes

<IfModule mod_rewrite.c>

    Options -Indexes

    RewriteEngine On
    RewriteBase /mpl/servicos/smvcf/

    # Force to exclude the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.*)/$
    RewriteRule ^(.+)/$ $1 [R=307,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php?$1 [QSA,L]

</IfModule>

I have a route that expects an id:
http://localhost/mpl/servicos/smvcf/detalhe/37343

But I need the URL the user sees to be friendly as:
http://localhost/mpl/servicos/smvcf/mercedes_benz-a-a_220_cdi_auto-37343.html

I thought something like this would work, but I get a 404:

RewriteRule ([^/]+)-([^/]+)-([^/]+)-([^/]+).html detalhe/$4

Please help.


Source: .htaccess

1 Comment

  1. Edwin

    If you are trying to use another rewrite that will not be routed through the default MVC route index.php then you need to make sure you place your new rewrite before those rules. Also that folder you are redirecting to will need to have a index PHP file to do something as well.

    
    Options -Indexes
    
    
    
        RewriteEngine On
        RewriteBase /mpl/servicos/smvcf/
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.+)-([0-9]+)\.html detalhe/$2 [L]
    
        # Force to exclude the trailing slash
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.*)/$
        RewriteRule ^(.+)/$ $1 [R=307,L]
    
        # Allow any files or directories that exist to be displayed directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?$1 [QSA,L]
    
    
    Reply

Leave a Reply