R
R
Robotex2010-12-24 04:10:24
PHP
Robotex, 2010-12-24 04:10:24

Modrewrite - an article for beginners?

Often I see beautiful links like:
site.domain/user-%user_id%
or
site.domain/user/%user_id%/
or
user%user_id%.site.domain/ I
guess that Apache ModRewrite, which I often talk about I hear, but I've never met him.
So how are such beautiful URLs made and how, when creating a new user, create a url for his profile?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
S
SwampRunner, 2010-12-24
@SwampRunner

like this:
#domain.com/user-66
RewriteRule ^(user\-)([0-9]+)$ /user.php?id=$2 [L]
#domain.com/user/66/
RewriteRule ^( user)/([0-9]+)/$ /user.php?id=$2 [L]
#user.domain.com
RewriteCond %{HTTP_HOST} ^([0-9a-z\-]+)\. domain\.com$ [NC]
RewriteRule ^$ /user.php?login=%1 [L]
The last option with virtual subdomains is only possible if you have access to the server settings.

D
Doktor_Gradus, 2010-12-24
@Doktor_Gradus

And it’s also easy to make your own 404, in the same .htaccess write
ErrorDocument 404 / error404.html
or

D
Doktor_Gradus, 2010-12-24
@Doktor_Gradus

or
ErrorDocument 404 /index.php?error=404
or whatever.

D
Doktor_Gradus, 2010-12-24
@Doktor_Gradus

In order to prohibit direct access to files in apache, there is the Deny command
. For example, to deny access to any folder (a real one), you need to put .htaccess with the Deny from all line in this folder.
But trying to reach such a folder is already a “403 error” “Access denied” and in general .htaccess
should already be written
ErrorDocument 403 / error404.html

R
Robotex, 2010-12-24
@Robotex

Is it possible to force it to throw parameters not by GET, but by POST?

I
Iskander Giniyatullin, 2010-12-24
@rednaxi

habrahabr.ru/blogs/webdev/101585/ - about your 3 points

M
m00t, 2010-12-24
@m00t

I believe that the rules of the form
RewriteRule ^(user\-)([0-9]+)$ /user.php?id=$2 [L]
as suggested to you in the comment above, although they are correct for this task, but in the consequences lead to a strong growth of .htaccess with the growth of the site - then you will want to make not only such urls for users, but also for the directory, and for articles something like "articles/title.html". A somewhat more general principle for creating beautiful CNCs is described in my comment here habrahabr.ru/qa/1488/#answer_6155

R
Robotex, 2010-12-27
@Robotex

Created this .htaccess
RewriteEngine on
RewriteBase /
#domain.com/mode/id/
RewriteRule ^(\w+)/([0-9]+)/{0,1}$ /index.php?mode=$1&id=$2 [L]
Deny from all
ErrorDocument 403 /index.php?error=404
ErrorDocument 404 /index.php?error=404
Now everything is forbidden.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question