user3038744
user3038744

Reputation: 86

Clean URL rewriting rule

right now my url looks like this:

http://domain.com/en/c/product%2C-product2%2C-product3/82 

where last number is category numer.

And im trying to rewrite it and redirect user to url which should look this one:

http://domain.com/82/product-product2-product3

The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting. Any ideas?

Upvotes: 1

Views: 133

Answers (2)

anubhava
anubhava

Reputation: 786091

You can use these 2 rules in your root .htaccess for that:

RewriteEngine On
RewriteBase /

RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]

RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]

Upvotes: 1

Tharif
Tharif

Reputation: 13971

In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.

To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.

The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").

If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule    ^url1/?$    url2.php    [NC,L]    # Handle requests for "url1"

Upvotes: 1

Related Questions