PHP .Htaccess -> Pretty Url (In Reverse)

PHP .htaccess - pretty url (in reverse)

RewriteEngine on

# Prevents browser looping, which does seem
# to occur in some specific scenarios. Can't
# explain the mechanics of this problem in
# detail, but there we go.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
# and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
RewriteRule ^index\.php$ http://example.com/index/%1/%2/? [L,R]

# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2

Of course, ideally, you'd just fix your links, and then you'd only require the soft-rewrite. :)

Tested with Apache/2.2.3. I think I made up the terms "hard-rewrite" and "soft-rewrite".

Rewriting url SEO friendly, how to reverse back?

Why not make an additional unique identifier in your database, so that a row of data has both a unique ID (7) and a unique "slug" (dog-food-purina)? That way you can look up rows any way you want by simply forwarding either the slug or the ID to your PHP script and handling it all there.

For example: your purina row can be as follows:
row -> id = 1
row -> name = "Purina Dog Food"
row -> description = "This is awesome stuff!"
row -> slug = "dog-food-purina"
row -> quantity = "10lbs"
row -> price = "5"

And then do something like this:

if (is_string($param) && !empty($param)) {
$sql = "SELECT * FROM products WHERE slug = '{$param}'"
} else if (is_numeric($param) && $param > 0) {
$sql = "SELECT * FROM products WHERE id = {$param}"
} else return false

Hope it helps

how to change URLs in cakephp through .htaccess

Using .htaccess rewrite rules would be quite pointless, since Cake would still generate all links "the Cake way", being completely oblivious to any rewritten URLs.

Instead, use Routes to configure special URLs for certain actions. These will be reverse-routable, meaning anywhere you tell Cake to make a link for array('controller' => 'foo', 'action' => 'bar'), it will use the configured short route. Example:

Router::connect('/foo', array('controller' => 'foo', 'action' => 'bar'));

echo $html->link('FooBar', array('controller' => 'foo', 'action' => 'bar'));
// <a href="/foo">FooBar</a>

Display Pretty URL in browser

You can use the following rules in /root/.htaccess

RewriteEngine on
#1--Redirect from "/videos/foo.php" to "/foo"--#
RewriteCond %{THE_REQUEST} /videos/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,NC,L,R]
#2--Internally map "/foo" to "/videos/foo.php--#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /videos/$1.php [NC,L]

Clear your browser's cache before testin this.

Htaccess /username rewrite conflict with existing mapped files

You can keep these 2 rules in the given order:

Options All -Indexes -MultiViews
RewriteEngine On

## To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

## for user profile
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]

It is important to turn off -MultiViews (content negotiation service) of Apache for this.

url rewriting in php localhost with .htaccess not working

To externally redirect a browser to the nicer looking URL (without the query string):

RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /Ent/Movie/Info\.php\?year=([^&]+)&movie=([^&\ ]+)([^\ ]*)
RewriteRule ^ /Ent/Movie/Info/%2/%3?%4 [L,R=301]

Then internally rewrite it back

RewriteRule ^/?Ent/Movie/Info/([0-9]+)/(.*)$ /Ent/Movie/Info.php?year=$1&movie=$2 [L,QSA]

These rules go in the document root, and they'd go right under:

Options +FollowSymLinks 
RewriteEngine on


Related Topics



Leave a reply



Submit