How to Make Clean Urls

How to make Clean URLs

You have the idea by using the rewrite rule, but you have missed out of the last bit.

RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1

The $1 will take the first varable entered so you can have /index/example and the word example can be pulled as a _get['id']

You can create multipule in this way:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?id=$1&login=$2

and so on.

Update:

If you just want to make the URL look nicer follow this link and read up on all the different ways of using the rewrite rule.

You can change http://www.pets.com/pet_care_info_07_07_2008.php

To look more like this: http://www.pets.com/pet-care/

If thats what you are looking for then the rule you want to use is:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"

Quoted from https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:

RewriteRule - Tells Apache that this like refers to a single RewriteRule.

^/pet-care/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.

pet_care_info_01_02_2003.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.

# Handle requests for "pet-care" - Comment explaining what the rule does (optional but recommended)

How to make clean url for html and javascript based sites

Since you use .htaccess I suppose that use WAMP/Apache on your localhost installation, but your web site is hosted in a server that runs on IIS (Microsoft-IIS/7.5 as reported in the headers), which uses another file for directory configuration.

You have two options:

  • If possible, change your host plan to another that uses Apache (some hosting providers offers Apache on Windows, if you don't want a Linux host for some reason). IMO this would be the best option in order to make your test environment as similar as possible to the server of your web site.

  • Use web.config, which is more or less the equivalent of .htaccess in IIS, check this link:

    .htaccess or .htpasswd equivalent on IIS?

How to create clean url using .htaccess

Replace your code with this:

ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+movie\.php\?name=([^\s&]+) [NC]
RewriteRule ^ movie/%1? [R=301,L]

RewriteRule ^movie/([^/]+)/?$ movie.php?name=$1 [L,QSA]

How to create Clean URL for every page using .Htaccess?

You mentioned WordPress, which has something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

What this does is redirect any request that doesn't match a real file or directory to the index.php script. That script will then decide what page to display. It will do so by looking into $_SERVER['REQUEST_URI'] which holds a path like register/part1.

It would be easier for you to figure out what page to show using this method, because in PHP there are many ways to parse that path string, then map it to a function

How to make a clean URL of this (htaccess URL rewrite)

Well you can make the friendly URLs work like this, and also redirect the old URLs:

# Rewrite new URLs
RewriteRule ^help/article/(.+)$ help/index.php?/selfhelp/view-article/$1 [E=rewritten:1]
RewriteRule ^help/categories$ help/index.php?/selfhelp/categories [E=rewritten:1]

# Redirect old URLs
RewriteCond %{ENV:rewritten} !=1
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteCond %{QUERY_STRING} ^/selfhelp/view-article/(.+)$
RewriteRule ^help/index\.php$ /help/article/%1 [R=301,L]

RewriteCond %{ENV:rewritten} !=1
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteCond %{QUERY_STRING} =/selfhelp/categories
RewriteRule ^help/index\.php$ /help/categories [R=301,L]

Clean URLs in a subfolder

You can use these rules inside your /subfolder/.htaccess

RewriteEngine On
RewriteBase /subfolder/

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]

Update (passing two values)

RewriteEngine On
RewriteBase /subfolder/

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]

RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]

# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]

# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]

Make clean URLS and retrieve query string

... carried on from OP comments.

These frameworks read the request again in their respective languages to allow the framework to route to specific controllers, but they still need the webserver to be setup to send the request to the framework in the first place.

  • Client requests http://example.com/forums/123
  • After DNS lookup, request hits server at 127.0.0.1:80
  • Webserver (eg Apache) listens for port 80 and accepts request
  • Apache routes request to server-side script

This is the journey as the web server sees it. It needs to read the request before it even hits the server-side scripting language (PHP/Python/Ruby etc).

The server-side languages can then re-read the URL once the webserver has hit the front controller as they please.



Related Topics



Leave a reply



Submit