Htaccess best practice

I want to get this final url:

'ideal url'

domain/services/city/name/

from this url:

'real url'

domain/page.php?s=services&c=city&a=name

This should work when the user enters the “perfect” URL, and when the “real” URL is clicked.

I would like this to work as well with domain/services/how domain/page.php?s=servicesor domain/services/city/how domain/page.php?s=services&c=city.

I am not asking for code, but what is the best practice for achieving this result.

Should I use only the htaccess and write commands for each case, or do I need to do something else?

+5
source share
5 answers

. . , , TinyMVC, google , , ..

, , 'c' 'a'. , , , :

'city' => 'c'
'name' => 'a'

, :

/domain/services/c=london/n=peter/

, php .htaccess( index.php), , swf, css, js ..

, URL. url.

, " ", " ", url, /module/action/param1/param2/ .. , , .

, htaccess, .

"" URL-, , .., .htaccess . / URL,

<a href="<?php buildUrl( 'modulke' , 'action' , 'param1' , 'param2' ); ?>">aaaa</a>

+1

mod_rewrite .htaccess httpd.conf, .htaccess DOCUMENT_ROOT:

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

# redirects /page.php?s=services&c=city&a=name to /services/city/name/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page\.php\?s=([^&]+)&c=([^&]+)&a=([^&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

# forwards /services/city/name/to /page.php?s=services&c=city&a=name
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /page.php?s=$1&c=$2&a=$3 [L,QSA]

PS: , /services/city/name/ URI. /services/city/ /services/ (1 1 URI, )

+1

, - php. htacess URL- /index.php?url=$bla /rewrite.php?url=$bla

. , .

, 100% - -php-, , , CSS, javascripts ..

0

.htaccess , .

:

 RewriteEngine On

 <IfModule mod_rewrite.c>
      RewriteEngine On 
      RewriteRule ^([^/.]+)/([^/.]+)/([^/.]*)?$ page.php?s=$1&c=$2&a=$3&%{QUERY_STRING}
  </IfModule>
0

http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ has all the answers you are looking for to get you started.

As pointed out by @Uberfuzzy, it is best practice to determine the most specific first like this:

RewriteRule    ^domain/services/city    domain/page.php?s=services&c=city    [NC,L]
RewriteRule    ^domain/services/        domain/page.php?s=services           [NC,L]

Usually you need to use htaccess and write commands for each case. The exception is to use a framework (Zend Framewor) or other tool that handles re-writing the URL for you.

0
source

All Articles