Forum Moderators: phranque
What I want to do is have a URL, such as http://www.example.com/members/ include another folder which does not exist on the server i.e. a virtual folder. What I have now is http://www.example.com/members?user=bob, where bob, would be the virtual folder (http://www.example.com/members/bob/). I am trying to clean things up, instead of having ugly URL strings for member pages.
The issue I am having is grabbing the virtual folder, and storing it in a $username variable in the http://www.example.com/members/index.php file. Is this possible?
If so, some of the usernames in the string may contain a full stop or dash. Will this cause issues?
Thanks.
[edited by: tedster at 7:14 pm (utc) on April 18, 2009]
[edit reason] switch to example.com - it cannot be owned [/edit]
You'll be using a RewriteRule I guess, coded as a rewrite, NOT as a redirect.
The URL will be www.example.com/members/bob and the internal filepath inside the server will be /members/index.php?name=bob I guess.
That's quite easy to code; there's an example like that almost every day here.
What code have you tried so far?
Do be aware that it is links that define' URLs, so you need to link to the www.example.com/members/bob URL format in the pages of your site.
Hyphens or dots will be OK in your URLs. Note that spaces and/or underscores would be a disaster.
.
Once the rewrite is coded and working, we will need to talk about adding additional redirects for canonicalisation and for avoiding Duplicate Content caused by direct access to the old format URLs.
RewriteEngine on
RewriteRule ^members/([a-z0-9][a-z0-9\-]{0,22}[a-z0-9])/$ /index.php?name=$1 [NC,L]
Jim
1. If the user enters http://www.example.com/members/name with no slash (/) on the end, the page gets a 404. With the slash it works.
2. When I include non alphanumeric characters such as . or - it breaks and causes a 404 also. Other than that, it works fine.
Note: I am using the .htacess file from within the members dir not the root dir.
Your help would be appreciated.
The rule can also be adjusted to allow other characters to appear in the URL. You didn't mention any other parameter value than 'bob' in your problem description, so nothing other than 'characters' was provided in the solution.
RewriteEngine on
#
# Externally redirect to forcibly remove trailing slash (canonical URL has no trialing slash)
RewriteRule ^([a-z0-9][a-z0-9.\-]{0,22}[a-z0-9])/$ http://www.example.com/members/$1 [NC,R=301,L]
#
# Internally rewrite slashless membername requests to /members/index.php script.
# Membernames may be up to 24 characters long, must start with a letter or number, may
# contain letters, numbers, periods, or hyphens, and must end with a letter or number.
RewriteRule ^([a-z0-9][a-z0-9.\-]{0,22}[a-z0-9])$ /members/index.php?name=$1 [NC,L]
Jim
RewriteEngine on
RewriteRule ^(.+)/$ /members/index.php?name=$1 [NC,L]
This works for urls with / on the end, but not with no forward slash. I tried the rules from jdMorgan, but they caused Internal server errors. The "^(.+)/$" has allowed my - & .'s to be included , and I can control the other variables elsewhere, but still need control on the end slash issue. Any ideas?
Thanks in advance.
You have changed the rewrite to accept URLs that do include a slash. That's not a good idea if the redirect is still in place to take it off. That's a conflict.
I recommend that you use the slashless URL as the canonical form, and redirect to that.
RewriteEngine on
RewriteRule ^(.+)/$ http://www.example.com/members/$1 [NC,R=301,L]
RewriteRule ^(.+)$ /members/index.php?name=$1 [NC,L]
Also, this rule does not like have query string items such as: http://www.example.com/members/test/?page=1
It redirects back to http://www.example.com/members/test?page=1, which is also causing issues, but I am assuming this is just because the rewrite is incorrect.
Any ideas?
Note that the page=1 parameter is 'lost' in the rewrite. You'll need to use the [QSA] flag if you need to get it re-appended on the end of the path.
RewriteEngine on
RewriteRule ^(.+)$ /members/index.php?name=$1 [NC,L,QSA]
This does not work.
Only the rule for trailing slashes works:
RewriteEngine on
RewriteRule ^(.+)/$ /members/index.php?name=$1 [NC,L,QSA]
If I can understand why I cannot write a rule to accept urls with no slash on the end this should be fine i.e. http://www.example.com/test?page=1
Any ideas?