Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

rewrite rule

Posted on 2010-11-12
18
455 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
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…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

809 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