htaccess rewrite index.php on root and subfolders - .htaccess

I'm trying to redirect all requests to ./index.php?site=$1 while I only want to use the part behind the last slash.
So I want www.mydomain.com/firstpage to become www.mydomain.com/index.php?site=firstpage and www.mydomain.com/subfolder/anotherpage to become www.mydomain.com/subfolder/index.php?site=anotherpage
But right now www.mydomain.com/subfolder/anotherpage becomes
www.mydomain.com/index.php?site=subfolder/anotherpage
This is what I've got:
RewriteEngine on
Options +SymlinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]
What could I do to redirect only the part after the last slash?
Thank you so much!
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]

Found this other, on my opinion more elegant solution. By this, the number of subfolders is irrelevant.
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]

Finally figured out the answer myself.
At first I tried putting more RewriteRules under the same RewriteCond-lines gave me server errors. So I duplicated the RewriteCond-lines for the subdirectories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1/index.php?site=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]

Related

htaccess remove parameter and index file

I'm trying to remove the proxy.php file and the parameter passed to it. This is what it looks like currently.
http://localhost/stuff/proxy.php?parameter=folder01/some.file
This is the way I would like it to look.
http://localhost/stuff/folder01/some.file
For my htaccess file, I have tried the following but they do not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy.php?parameter=$1 [L]
And this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /proxy.php?parameter=$1 [L,QSA]
Any ideas?
Try it in root dir or put this in stuff directory and remove RewriteBase line.
RewriteEngine on
RewriteBase /stuff/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ proxy.php?parameter=$1 [L]

rewriterule dynamic with sub

I can't fix this issue. I'm trying to get rewrite rules to work with dynamic urls like:
http://example.com/[CATEGORY]/[PAGE]/[OPTIONS]
or
http://example.com/[PAGE]
This is my .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])
RewriteRule ^(.*[^/]) /index.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])\/^(.*[^/])
RewriteRule ^(.*[^/])\/^(.*[^/]) /index.php?url=$1/$2 [QSA,L]
The first rewriterule is working, but the second isn't. Also is this not clean.
Anyone who knows this?

.htaccess call PHP file with parameters

Sorry if the title of the question is not clear, please read below for details;
I have this .htaccess:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
One of its functions is to redirect the url from this example:
www.example.com/user.php?user=david
to be:
www.example.com/david
Now, I have a file friends.php which function is to show friends of a particular user.
So the link that I wanted is like this:
www.example.com/david/friends
How can I achieve this?
You have to basically do the same as you did for user. In fact, the rule does not even conflict, because the user rule cannot contain a / character.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/friends$ friends.php?user=$1 [L]

htaccess - rewritecond to check for slash

I have a rewriterule on my site thats like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ open.php?url=$1 [L]
(There is a lot above it but this is the last rule)
It works very nicely.
What I want to achieve, however, is that if the url is as follows:
http://site.com/first/second
Then I want to redirect to
open.php?url=$1&open=$2
The problem is that I want to have both of them at the same time so that site.com/foo works as well as site.com/foo/bar . I also want to avoid having a trailing slash there all the time ergo both of the rules at the same time.
If there a way to check for trailing slashes with a rewritecond of some sort
any help is appreciated :)
SOLVED
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(.*)/(.*)$
RewriteRule ^(.*)$ open.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/(.*)
RewriteRule ^(.*)/(.*)$ open.php?url=$1&open=$2 [L]
All I had to add was a RewriteCond %{REQUEST_URI} !^/(.*)/(.*)$ on the first rule!

.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]

Resources