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]
Related
The multiple RewriteRule's doesn't work into my .htaccess file.
To get direct into the point, i have this lines of code into my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ ./profile.php?page=$1 [L]
The problem is when i add a parameter into my domain, let's suppose www.eaxmple.com/something, i land always to home page. What i want to do is when i set a parameter with slash at the end to go to profile.php and without slash to move into index.php. Even if i tried to put a parameter i always move to index page.
Can someone help me?
.* will match everything including trailing /.
Try rules in this order:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L][
RewriteRule ^(.+)/$ profile.php?page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?lang=$1 [L,QSA]
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.
I need help with this rewrite in .htaccess file.
So this what I have now ans this works but when I try to add a new RewriteRule nothing happens.
I the url that I want to be rewrite is index.php?page=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
So when I do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
The page doesn't have any css when i do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1
The page has css but i still get index.php?page=pagetitle. But the profile page does give me /username.
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
Your are asking the server to redirect every URL to two different pages, it cannot work the server cannot just guess what page to load.
What you need is either a /profile/username rule or a /page/pagetitle rule.
IE something like:
RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
Your rewrite rules are based on regular expressions and therefore need to be as specific as possible so the server can determine exactly which url to use - for example how can you tell if http://example.com/something is a page or a profile? Using a prefix such as "user", "profile", etc on your URLs means that http://example.com/profile/something can be redirected as as a username with a default redirect for everything else. To accomplish this you need to make the more specific pattern match first (users) and utilized the [L] directive to indicate that following rules should not be processed. I usually use a negative character class for URLs to match anything except a forward slash - [^/]*.
# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Test at http://htaccess.madewithlove.be/ (note that %{REQUEST_FILENAME} and %{REQUEST_FILENAME} aren't supported for testing).
Profile
input url
http://www.example.com/profile/something
output url
http://www.example.com/profile.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]
This rule was met, the new url is http://www.example.com/profile.php
The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Page
input url
http://www.example.com/something
output url
http://www.example.com/index.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
This rule was met, the new url is http://www.example.com/index.php
The tests are stopped because the L in your RewriteRule options
Here is what I am trying to do:
When a file is requested from filesystem and it does not exist, rewrite the URL to /index.php?404
When file is requested and it does exist in filesystem, rewrite the URL to /index.php?file
In every other case rewrite the URL to /index.php?data
But I am getting 500 errors as a result, does anyone know where the problem might be? I have used RewriteEngine in the past, but it's still a bit confusing to me regarding how to use it for special cases like this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (.*\.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?404 [L]
RewriteCond %{REQUEST_FILENAME} (.*\.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* ./index.php?file [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?data [L]
You have infinite rewrite loop. To solve -- add extra condition to not rewrite already rewritten URLs .. or at least ignore requests to index.php.
One of the possible approaches:
# do not touch any requests to index.php
RewriteRule ^index\.php$ - [L]
P.S.
How L flag works: RewriteRule Last [L] flag not working?
I want to redirect what's in link after localhost/myScript/ to localhost/myScript/test.php?var=$1.
So.. something like
localhost/myScript/this/is/just/a/test/
(with or without the last slash) would redirect to
localhost/myScript/test.php?var=this/is/just/a/test
This is what I've got, but it's not good enough.
RewriteRule ^(.+)$ test.php?var=$1
I've tried using
RewriteRule ^(.+)/?$ test.php?var=$1
But, I get $_GET['var'] = 'test.php'.
I see 2 main approaches:
1. Two separate rules to deal with URL with and without trailing slash. It will only rewrite requests to non-existing files/folder thus preventing rewrite loop you are having:
# work with URL that ends with slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ test.php?var=$1 [L]
# work with the rest of URLs (that ends with no slash)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ test.php?var=$1 [L]
2. Have separate rule to ignore existing files (to not to rewrite already rewritten URLs) and then use 1 line rewrite rule for URL with and without trailing slash:
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite URLs
RewriteRule ^(.*[^/])/$ test.php?var=$1 [L]
RewriteRule ^(.+)$ test.php?var=$1 [L]
I would prefer #2.
this is better one
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.+?)$ test.php?var=$1 [L]