Solved

rewrite rule

Posted on 2010-11-12
18
452 Views
Last Modified: 2012-05-10
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
Comment
Question by:eranhazout
  • 8
  • 8
  • 2
18 Comments
 
LVL 5

Expert Comment

by:SimonDard
ID: 34120949
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
 

Author Comment

by:eranhazout
ID: 34121106
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
 
LVL 5

Expert Comment

by:SimonDard
ID: 34121181
I see I made an error: switch the last two rules.
0
 

Author Comment

by:eranhazout
ID: 34121227
still the same after switching the last two rules

0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34125772
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
 

Author Comment

by:eranhazout
ID: 34127323
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
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34127980
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
 
LVL 16

Accepted Solution

by:
HackneyCab earned 500 total points
ID: 34128038
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
 

Author Comment

by:eranhazout
ID: 34130505
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Expert Comment

by:HackneyCab
ID: 34130954
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
 

Author Comment

by:eranhazout
ID: 34131088
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
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34131112
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
 

Author Comment

by:eranhazout
ID: 34131168
I get: Internal Server Error

0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34131305
For which requests, all or only for admin?
0
 

Author Comment

by:eranhazout
ID: 34131315
for every request ...
home page. profile page etc'
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34133319
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
 

Author Comment

by:eranhazout
ID: 34135053
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
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34136658
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

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now