• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

rewrite rule

I have a rewrite rule for www.domain.com/user-name to www.domain.com/profile.php?n=user-name.

I need a new rule for www.doamin.com/p/user-name to www.domain.com/small-profile.php?n=user-name.

how can I do it ?

this is what I have on htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9.-]+)/?$ profile.php?n=$1 [L]
0
eranhazout
Asked:
eranhazout
  • 8
  • 8
  • 2
1 Solution
 
SimonDardCommented:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

RewriteRule ^([a-zA-Z0-9.-]+)/?$ profile.php?n=$1 [L]
RewriteRule ^p/([a-zA-Z0-9.-]+)/?$ small-profile.php?n=$1 [L]
0
 
eranhazoutAuthor Commented:
both of them are pointing to profile.php.

it should point /p/user-name to small-profile.php
and point /user-name to profile.php

0
 
SimonDardCommented:
I see I made an error: switch the last two rules.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
eranhazoutAuthor Commented:
still the same after switching the last two rules

0
 
HackneyCabCommented:
Try this:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9.-]+)/?$ /profile.php?n=$1
RewriteRule ^p/([a-zA-Z0-9.-]+)/?$ /small-profile.php?n=$1

Without the forward-slash anchoring the target to the top-level directory, you run the risk of relative paths which don't point to where you intend.

Also be aware that you must now forbid any user from choosing the username "p", otherwise you make it impossible to work out which rule should be followed.

I don't understand what your first two rules (checking for -f and -d) are doing.
0
 
eranhazoutAuthor Commented:
I tied this and it cause internal server error...
I'm not sure about the first two rules, I just find that in some forum ...

any other idea ?
0
 
HackneyCabCommented:
I just tried the rules on my test rig, and they work as expected, so I suspect you have other rules which are causing the error. If you're able to post all of the mod_rewrite directives found in your .htaccess here, we can look for any traps.
0
 
HackneyCabCommented:
Actually, I've just realised that (because you allow usernames which contain a period) the username pattern will also match profile.php and small-profile.php, so those requests are being redirected, and this goes round and round infinitely, causing the Internal Server Error.

If your usernames do not actually permit periods, then remove the . from the pattern in both rewrite directives. If you do need to permit periods in usernames, then you may need to add back in the -f and -d checks.
0
 
eranhazoutAuthor Commented:
now I have a new bug with this ...

when I try to go to admin, it goes to profile.php ...
it seams like the script is not checking if this is a real directory ...


any ideas ?
0
 
HackneyCabCommented:
One of the many reasons I don't like these "pretty" URLs, they cause trouble like this.

However, you can fix this by adding a RewriteCond before BOTH of the RewriteRule directives:

RewriteCond %{REQUEST_FILENAME} !-d

If you also need to permit valid filenames (if you have any actual files in the top-level directory) then you will also need this before BOTH RewriteRule directives:

RewriteCond %{REQUEST_FILENAME} !-f

These rules say "only use the next RewriteRule directive if the REQUEST_FILENAME is NOT a directory/file".
0
 
eranhazoutAuthor Commented:
thank you for replying ...
now both of them are not working ...

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]

RewriteRule ^pro-p/([a-zA-Z0-9-_]+)/?$ small-profile.php?n=$1 [L]
RewriteRule ^([a-zA-Z0-9-_]+)/?$ profile.php?n=$1 [L]

thank you
0
 
HackneyCabCommented:
Sorry, I should have said "before each RewriteRule directive". Like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pro-p/([a-zA-Z0-9-_]+)/?$ small-profile.php?n=$1 [L]

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

The "RewriteRule .* - [L]" directive is not necessary.
0
 
eranhazoutAuthor Commented:
I get: Internal Server Error

0
 
HackneyCabCommented:
For which requests, all or only for admin?
0
 
eranhazoutAuthor Commented:
for every request ...
home page. profile page etc'
0
 
HackneyCabCommented:
Just tried it on my test rig, and again it works as expected. With the -d and -f conditions, it correctly skips requests for actual pages/directories, and it works with both the profile and small profile requests.

The only thing I can spot that might be causing an error is that the hyphen has snuck into the second-to-last position in the character class, and if a hyphen needs to be an actual character (rather than a range delimiter) in a character class, then it must appear as the final character. For instance [a-z0-9_-] rather than [a-z0-9-_]. Possibly this is causing an error on your box but not mine (different versions, perhaps). Also, the root slash has vanished from the target path, and this is risky. So try this instead:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pro-p/([a-zA-Z0-9_-]+)/?$ /small-profile.php?n=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /profile.php?n=$1

(also make sure than "RewriteEngine On" only appears once in the .htaccess file, just in case.)
0
 
eranhazoutAuthor Commented:
Hi

I have tried this on server ...
it works fine, but still one problem with this script ...
when I'm trying to go to admin (physical directory) I get /admin/?n=admin

how can I get rid of that ?

Thank you
0
 
HackneyCabCommented:
You must have other rewrites in one of your .htaccess files, because neither of the above RewriteRule directives is capable of adding a query string to that path (only to the profile.php and small-profile.php rewrites).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 8
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now