?
Solved

rewrite rule

Posted on 2010-11-12
18
Medium Priority
?
462 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 2000 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
 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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