I'm no regex expert and I'm trying to implement on a website a social login system that may or may not redirect the user to the page where he was before loggin in.
Like so, for no redirection:
RewriteRule ^login/([^/]+)$ login.php?p=$1 [L]
http://example.com/login/Facebook >> the page does it stuff and goes to index (default behaviour).
or like this for redirection (p = provider; r= redirect relative path):
RewriteRule ^login/([^/]+)/([^/]+)$ login.php?p=$1&r=$2 [L]
Works great if the user is on a page like http://example.com/info that is generated by the following rule:
RewriteRule ^info$ users_infosystem.php [L]
I'm having problem when the user is on a page like http://example.com/gallery/galleryname
RewriteRule ^gallery/([^/]+)$ galery.php?name=$1 [L]
Any Help?
EDIT: Just occured me ... I don't know if the $ char is the delimeter that marks the end and taking it off makes the rule accept everythings that is after the ([^/]+) bit.
Also: Doesn't the ([^/]+) bit match everything but a forward slash?
I'm having problem when the user is on a page like
That is a very vague sentence. You don't actually say what the problem is. What about your rule is not working? Also didn't paste your entire htaccess file, you pasted bits and pieces so we don't even know what order your rules are in or if there are other rules.
I don't see an issue with these rules. These rules should work. They all match something different. Also you can make the / optional using the ? after it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/([^/]+)/?$ login.php?p=$1&r=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/?$ login.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/?$ users_infosystem.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([^/]+)/?$ galery.php?name=$1 [L]
The last rule will match http://example.com/gallery/galleryname with or without a / at the end.
Related
I have user.php file and I m passing two parameters with it using URL Rewrite
but I m unable to use this rule as whichever rule is written first only gets executed and not the second one in case of my rule for userID rule doesn't work but work for webName. Any solution will be very much helpful to me.Thank you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ user.php?webName=$1 [L,QSA]
RewriteRule ^user/(.*)$ user.php?userID=$1 [L,QSA]
Make a difference between two regural expression:
If you would like to catch user ID you should filter for only numbers: \d
Then if there is another chars in url not only numbers you can match for anything except kslash: [^/]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\d+)$ user.php?userID=$1 [L,QSA]
RewriteRule ^user/([^/]+)$ user.php?webName=$1 [L,QSA]
This is my htaccess code:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)$ http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Obviously I want this URL:
www.example.com/username
to be translated to http://www.example.com/index.php?id=82&user=username
This does not work however.. (this code results in the htaccess not working at all and getting a Page not found error.
If I change the ]+$ for a ]+? the code does work, but not like I want:
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301]
Results in the URL being rewritten/redirected to http://www.example.com/index.php?id=82&user=index ... Exactly like that, so with user=index.
Now, if I remove the RewriteRule .* /index.php line, the htaccess again doesn't work at all anymore, resulting in a Page not found error...
I've spent days and days on figuring this out but I'm absolutely clueless..
So, I just want www.example.com/username to redirect to http://www.example.com/index.php?id=82&user=username
There are several issues here.
Inside a .htaccess file, patterns are matched "against the filesystem path, after removing the prefix". This means, you will have no leading slash as in /typo3 or ^/([a-zA-Z0-9_-]+)?
The pattern ([a-zA-Z0-9_-]+)? matches also an empty request, because of the trailing ?. I guess, this is not what you intended.
Rules are processed in sequence, unless you do a redirect [R] or add an [L] flag. This is the reason why first the request is rewritten to index.php and then in the next rule index is recognized as the user and again rewritten to .../index.php?id=82&user=index
Which leads to the next problem between the patterns .* and ([a-zA-Z0-9_-]+). .* recognizes all requests, including every user. So there is no way to distinguish between a user and any other request.
To rewrite usernames, you could try
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+) http://www.example.com/index.php?id=82&user=$1 [L]
This means, if the request doesn't correspond to an existing file !-f or directory !-d, then presume it's a username and rewrite to index.php?....
If you don't want a redirect, leave out the host name
RewriteRule ^([a-zA-Z0-9_-]+) /index.php?id=82&user=$1 [L]
I have created a .htaccess file that allows for vanity URLs. However, I am no longer able to type www.website.com without typing in the index file. I.e. I have to type in www.website.com/index.php in order to see the homepage. This is what my .htaccess file looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
Anyone know how to fix this? Thank you all!
The way you defined your rule increased the complexity.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Above rule means if file name is a directory of file process as it is. after that dont process farther.
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
This rule means map any request uri to profile.php?u=
Now when you request / that is www.website.com it checks the first rule and it fails to match. Then it check the second rule and maps it to profile.php?u=.
One way to fix it, would be check *if $_GET['u'] is empty or / in profile.php. If it is then load the index.php.
Another way is to find a proper regular expression for your user names once found use it here.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(USERNAME_REGEX)$ http://www.website.com/profile.php?u=$1 [NC,L]
The best way to handle this is using PHP,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?uri=$1 [L]
Now index.php will get every uri you pass. Now you can process the URI in index.php.
It may be due to your server set up.
Try DirectoryIndex index.php (see http://davidwalsh.name/directory-index-homepage-htaccess )
Edit (due to me not reading the question properly in the first place)
Have you tried it without the RewriteRule .* - [L] line?
I am currently using .htaccess to rewrite my urls from
example.com/mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)
The code i use to accomplish that is below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/?$ mixtape.php?mixid=$1 [L]
The next thing I am trying to achieve, in a clean matter, is to rewrite my
example.com/edit-mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)/edit
EDIT: the "WHATEVER #" is the ID of the Mixtape I am editing. Normally i will use "$_GET['id']" to see what mixtape i'm referring to and then i fetch everything related to that number.
IS there anyone out there that would be able to help me successfully write a proper re-write mod?
Based on your comment, I would change your original rule as well to allow only for numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/?$ mixtape.php?mixid=$1 [L]
By the way, I would probably use a general rule and write both urls to the same php file and handle the action in the controller.
You can use the same rules that you had before but just append an edit to the regular expression:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
Note that your original rules have the query string mixid= while your examples say id=.
I've got most of the rewrites I need working but I can't get the second part working with 2 variables, here is my code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ entity.php?vanityName=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /entity.php?vanityName=$1§ion=$2 [L]
The last bit needs to allow, for example, http://example.com/vanityName/Section however it doesn't pass the section variable.
How can I fix this while retaining the current rewrites?
How can I get it so it works for /vanityName, /vanityName/section but allows me to keep other directories free of rewriting, like /includes/?
You can add an extra rule for every directory you don't want to rewrite:
RewriteRule directoryName/? - [L]
If you write these rules directly after RewriteBase /, no other rules will be checked.