.htaccess Rewrite QSA - .htaccess

So I have an htaccess file that I'm working on for someone, and I'm stuck on passing the URL arguments. Basically any and all arguments need to be passed, however I don't know all the queries, which is where I get lost and need help.
I've literally spent the past hour and a half digging through the internet and read about QSA's but have just gotten completely lost... Needless to say, I'm not well with htaccess files. Lines 1-23 are for something else related, but with the pages themselves.
Here's a few examples in case you're confused:
"/events/?query=dark needs to execute events.php?query=dark"
"http://mywebsite.com/authorize/?name=john would actually execute http://mywebsite.com/authorize.php?name=john"
Here's what I have so far, line 25 and down is just an official query I have atm.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ "domain"/$1 [R=301,L]
ErrorDocument 404 /404.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /events/?$ [NC]
RewriteRule .* /events.php [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /authentication/?$ [NC]
RewriteRule .* /authentication.php [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /databases/?$ [NC]
RewriteRule .* /databases.php [R=301,NC,L]
RewriteCond %{QUERY_STRING} name=(.*)
RewriteRule ^authorize/(.*) /authorize.php?name=%1

Related

.htaccess not every rewriterule works

I have the following .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^help$ /pages/help/index.php [NC]
RewriteRule ^help/search$ /pages/help/search.php [NC]
RewriteRule ^help/support$ /pages/help/support.php [NC]
RewriteRule ^help/bugfixes$ /pages/help/bugfixes.php [NC]
RewriteRule ^help/([a-z0-9-]+)$ /pages/help/category.php?cat_slug=$1 [NC]
RewriteRule ^help/([a-z0-9-]+)/([0-9]+)/([a-z0-9-]+)$ /pages/help/article.php?cat_slug=$1&art_id=$2&art_slug=$3 [NC]
The problem is the following:
When I go to localhost/help, it redirects to localhost/C:/server/htdocs, so I don't see the index page. The same happens when i go to localhost/help/bugfixes.
The other pages work perfectly! And untill today, /help and /help/bugfixes did also. :/
If I change ^help/bugfixes$ to ^help/bugfix$ (for example), the bugfixes-pages is showed again. If I change ^help$ to ^help/help$ (or any other thing), it also works for the index page.
I have literally no idea what could be wrong. I restarted apache already a couple of times, but still nothing... I didn't changed anything in the file for the last 2 days.
Can someone help me?
Edit: I also didn't changed anything in the php-files index.php and bugfixes.php
Try this htaccess :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^help/(.*)$ /pages/help/$1.php [NC,L]
RewriteRule ^help/([^/]+)/?$ /pages/help/category.php?cat_slug=$1 [L]
RewriteRule ^help/([^/]+)/([^/]+)/([^/]+)/?$ /pages/help/artical.php?cat_slug=$1&art_id=$2&art_slug=$3 [L]

htaccess displays error 500 when adding more than 3 rules

this is my htaccess:
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home index.php
RewriteRule ^register register.php
RewriteRule ^rules rules.php [L]
When i add fourth rule to htaccess it returns 500 error.
so htaccess is allowing only 3 rewrite rules?
First thing is when you receive an error you should always check your Apache error_log file. It will give you the specific reason for the error. We would only be guessing at what your problem might be.
Now this might not be "the" issue however the RewriteCond only works for the RewriteRule directly following it. You can't use the same condtions for all of your rules. You should also get in to the habit of using the [L] flag on the rules to prevent it from reading the next rule once it has matched.
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/?$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/?$ register.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rules/?$ rules.php [L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

htaccess is not ignoring images like it should?

I have an htaccess file that is supposed to be redirecting any non-existing files/folders/etc into the application's index.php script where they are handled by the application's SEO rewriting.
The problem is that any missing images are also redirecting into the application which is causing a lot of additional load and is unnecessary. I've been attempting to get this htaccess file to ignore images, but even though this should be working, it's not, and I'm completely out of ideas as to why...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
When I make a request to http://www.domain.com/folder_that_doesnt_exist/image.jpg this redirects to index.php
When I make a request to http://www.domain.com/folder_that_does_exist/image.jpg it also redirects to the index script
I'm not sure what I'm missing here because unless OR is specified, shouldn't the RewriteRule only be applied if the request passes all of the RewriteCond statements? which it clearly should not be passing...
Update:
I've modified the code to the following just to eliminate possible issues, but it still redirects all non-existing images into the script...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
Maby try this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteRule .* - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

htaccess mod rewrite - how to create virtual subdomains?

I would like to create virtual subdomains through htaccess in the following way.
Entering:
http://testuser.domain.com/1/2/3/
Should be processed as:
http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3
HOWEVER, this rewrite should not use user.php, but index.php, in case someone enters:
http://www.domain.com or http://domain.com
This is what I got so far, however it doesn't seem to work.
Any help from a mod rewrite expert would be much appreciated.
Thanks in advance.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
Your rules are actually very close to doing what you want them to. The only problem that I can see is in your test patterns for your two RewriteRule statements. Currently, you have
RewriteRule ^(.*)/?(.*)/?(.*)/?$ ...
...which happens to be equivalent to this:
RewriteRule ^(.*)$
This is because everything past the first capture group can match nothing and still be considered a match, so that first group is greedy and matches the whole input string without needing to defer to the other parts of the pattern.
Since the capture groups shouldn't capture forward slashes anyway, as they're being used as a variable delimiter here, the straightforward fix is to change them to [^/]*, as so:
Edit: I also modified the RewriteCond set in the second group to ignore the !-f condition in the case of /index.php, which will happen if you request the subdomain without anything after the domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]

Resources