Mod rewrite user URL - php

I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance

I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.

Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.

Related

RewriteRule different pages per user

I am deciding to create separate profile links for each user who registers on the website. I am using the .htaccess file RewriteRule to achieve this. The url I want to achieve should look something like www.domain.com/username. (I don't want www.domain.com/users/username)
Now the problem is, if I add a rule like RewriteRule ^(.*)$ /users.php?username=$1
it will matchup all URL addresses for www.domain.com, which may direct to any path. Even if I would like to visit www.domain.com/about page, it will be redirected to
www.domain.com/users.php?username=about, which I don't want. (Even if the requests was www.domain.com/products/abc)
I could apply filters in my users.php to filter such usernames, or if a username is not found in database, redirect to the page, but this means I have to change filters every time I decide to add a new page in my directory (in this case, any). Also, all traffic will be directed through 1 page, which may cause a performance problem.
Please suggest a way to achieve this, as There are websites that work like this. A possible RewriteRule or PHP code to achieve this.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /users.php?username=$1 [L,QSA]
I always use just simple rewrite as below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
All traffic is redirected to index.php and using php I can run specific controllers depending on url. You should also think about. Almost all frameworks use such rule.
Then in your PHP file you should examine
$_SERVER['REQUEST_URI']
variable to route request to specific controllers

Rewrite URL Parameter containing dot

I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]

Rewritten URL shows 404 errors

I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.

Getting variable from pretty url

I've done things very similar before but for some reason I'm having a little difficulty with the specific scenario. I want to pass a folder path as a variable and make it look pretty.
I have a working url like:
http://mysite.com/albums/index.php?p=folder/subfolder/
I can view it without the 'index.php' like:
http://mysite.com/albums/?p=folder/subfolder/
What I want is a pretty url that looks like this:
http://mysite.com/albums/folder/subfolder/
Basically, anything after /albums/ should be a single variable. I've played with my .htaccess RewriteRule a bunch and can't seem to get it working. (get 404 errors) This is what I currently have:
RewriteEngine On
RewriteRule ^albums/(.*)$ albums/?p=$1
below is what i use though every call is directed to my index.php file and from there i do anything with it
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Hope it helps
Change (.*) into (.+) to ensure there is at least one character after / , otherwise your rule loops...

Guidance on URL rewrite and construction

I am about to attempt writing of a photo sharing script and a script/rewrite that transforms numbers into descriptive names. I have a vague idea on how to go about doing this, so I was looking for some general comments/guidance.
Issue 1: I need to have a URL source for a photo which is stored above my root directory. I plan on appending the photo name (which is stored in my database) to my url as a query string, such as: www.mywebsite.com/getphoto.php?12_3.jpg and then writing a php script (getphoto.php) which takes the portion after the '?' and gets that photo from above the root.
Does this make sense and would there be any things to consider?
Issue 2: I want to transform a number at the end of my URL to a descriptive name (ie typing in facebook.com/4 displays facebook.com/zuck). I am not really sure the best way to go about doing this and was hoping for some guidance to get going in the right direction.
Thanks!
For Issue 1: a simple rewrite can handle that, you need to use the [QSA] flag. Something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^.*\.(jpeg|jpg|gif|png|bmp)$
RewriteRule ^(.+)$ /getphoto.php?photo=$1 [L,QSA]
This will rewrite behind the scenes the url http://mywebsite.com/12_3.jpg to http://mywebsite.com/getphoto.php?photo=12_3.jpg Note that the 3rd rewrite condition wants the URI to end with an image extension, you may not need it.
For Issue 2, it depends on how something like "4" maps to "zuck". If you are going to hardcode them into your apache config, you can use a RewriteCond:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/4$
RewriteRule ^.*$ /zuck [L]
RewriteCond %{REQUEST_URI} ^/5$
RewriteRule ^.*$ /mark [L]
etc. (or replace [L] with [R,L] to redirect instead of rewrite, or alternatively just use Redirect)
Redirect /4 /zuck
Redirect /5 /mark
etc.
If the mapping is stored in a database, your going to need to do this dynamically, perhaps as a php script to do a redirect, utilizing something similar to Issue 1. The rewrite rule would rewrite to something like /redirect.php?id=$1 and your redirect.php script would take the id and do a database lookup to see where to redirect the browser.

Categories