Viewed   125 times

I'm having a hard time with .htaccess. I want to create friendly URLs for a site I'm working on...

Basically I want to convert this:

Into this:

In case I need it later I would also want to know how to add the article title to the end of the URL like this (I'm using PHP):

Note: Stackoverflow method is also good for me, for example the url of this question is:


But you can put anything after the id and it will also work. like this:


I have used some automatic web tools for creating the .htacces file, but its not working correctly. So I ask for your help.

I will also be glad if you can recommend .htacces best practices and recommendations..

EDIT: based on some answers I get here I put this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/([^/]+)/([^/]+)/?([^/]*)/?$ index.php?ctrl=$1&id=$2&tab=$3 [QSA,L]

But I get the default host 'page not found' error.

I also tried:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/(d+)/([^/]+)/?$ index.php?ctrl=$1&id=$2&tab=$3 [QSA,L]
    RewriteRule ^([^/]+)/(d+)/?$         index.php?ctrl=$1&id=$2 [QSA,L]
    RewriteRule ^([^/]+)/?$               index.php?ctrl=$1 [QSA,L]

This also does not work. It takes me to my default 404.php page.

mod_rewrite is enabled and working.




In the document root for I'd put an htaccess file like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Then in your PHP script you can manipulate the $_GET['url'] variable as you please:

$path_components = explode('/', $_GET['url']);
Wednesday, October 26, 2022

You can check this out:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./profile.php?username=$1 [L]

You can copy the above code and paste it on your .htaccess file. The profile.php is your php file that will read the username. You can check it out by creating a profile.php and use

var_dump($_GET); die();

and you will see the variable $_GET['username'] and from there you can now query your sql. * Just be careful for the sql injection and add some security.

Wednesday, December 7, 2022

Edit: adjusted to handle both the redirect and the rewrite.

RewriteEngine On 
RewriteBase /

# Redirect .php URLs to rewritten URLs
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+).php$ $1 [L,QSA,R=301]

# Rewrite URLs for processing by router (index.php)
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NC]

You should exclude the RewriteCond %{REQUEST_FILENAME} !-d condition, as it will attempt to access a directory if your URL matches it. Probably not desirable when doing url rewriting.


$url = isset($_GET['url']) ? $_GET['url'] : null;
Friday, September 30, 2022

It turns out this can be done with a relatively simple change to a single file. No .htaccess rewrite rules, simply patch the catalog/controller/common/seo_url.php file and add your pretty URLs to an existing database table.

The patch to seo_url.php:

Index: catalog/controller/common/seo_url.php
--- catalog/controller/common/seo_url.php   (old)
+++ catalog/controller/common/seo_url.php   (new)
@@ -48,7 +42,12 @@
                $this->request->get['route'] = 'product/manufacturer/product';
            } elseif (isset($this->request->get['information_id'])) {
                $this->request->get['route'] = 'information/information';
-           }
+           } else {
+                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
+                if ($query->num_rows) {
+                    $this->request->get['route'] = $query->row['query'];
+                }
+           }

            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
@@ -88,7 +87,15 @@

-                   }
+                   } else {
+                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
+                        if ($query->num_rows) {
+                            $url .= '/' . $query->row['keyword'];
+                            unset($data[$key]);
+                        }
+                   }

There are two edits required. The first extends the index() function to look in the url_alias table for any keyword matching $this->request->get['_route_'].

The second extends the rewrite() function to look in the url_alias table for all routes, not just those for products, manufacturers, and information pages.

Adding entries to the database:

INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) VALUES
(NULL, 'checkout/cart', 'cart');

That's it. should return the same thing that does, and OpenCart should recognize $this->url->link('checkout/cart'); and return a link to the pretty URL

Monday, August 1, 2022

Use ASP.NET routing rather than rewriting when possible. It's available with both MVC and Web Forms. Routing is much more flexible and does a better job in passing context to the processing code, handling postbacks, etc.

You can also use the IIS7 Rewrite Module to handle rewriting at the webserver level before your ASP.NET code executes. There's some good information on how to do that here.

Wednesday, September 28, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :