0

probably my question duplicate with an other title but, I have read too many title and checked many times with google. So if it duplicates, I am sorry.

Now I have an URL:

www.mysite.com/profile.php

I am using this URL rewrite rule:

RewriteRule ^([A-Za-z0-9-_]+)/?$    profile.php?username=$1 [NC,L]

And I can change my url like:

www.mysite.com/username

Then, I need to update my url with an other get parameter:

www.mysite.com/username/photos

At this part, I have an URL like:

www.mysite.com/profile.php?username=xxx&w=photo

I have tried this URL rule for the url for above:

RewriteRule ^([A-Za-z0-9-_]+)/?$/?$    profile.php?username=$1&w=$2 [NC,L]

But It does not work. Please help.

Thank you very much.

-------UPDATE------

Now I can use profile.php which It should be. But other rewrite rules are broken. My current .htaccess file like this:

RewriteEngine On

RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
RewriteRule ^p/timeline timeline.php [NC,L]
RewriteRule ^p/notifications notifications.php [NC,L]

3 Answers 3

1

First thing first:

[A-Za-z0-9-_]

is not a correct regex because of unescaped hyphen between 9 and _ in a character class that acts as a range between hex 39 and hex 5f. To fix this make sure to use hyphen at first or last position in a character class.

Correct rules will be:

RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]

Update:

As per your updated question make sure to use generic rule after specific rule. So have your rules like this:

RewriteRule ^p/(notifications|timeline)/?$ $1.php [NC,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
Sign up to request clarification or add additional context in comments.

Comments

0
RewriteRule ^([A-Za-z0-9-_]+)/(.+)$    profile.php?username=$1&w=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)$    profile.php?username=$1 [NC,L]

1 Comment

It work but again I have a problem. When second get arguments is null, this rule does not work. I want to use mysite.com/username and mysite.com/username/details at the same time. So how many rules should I have?
0

You can just have one rule if you want. I would also add the conditions so that it makes sure it's not a real directory or file.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?([A-Za-z0-9-_]+)?/?$ /profile.php?username=$1&w=$2 [NC,L]

In your PHP you will just have to check the w parameter to see if it's empty or not. If it's not, display the details also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.