.htaccess call PHP file with parameters - .htaccess

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]

Related

.htaccess multiple rewrite rules

I currently have a .htaccess rewrite rule that will redirect all urls containing /ws to the /ws/index.php eg. www.domain.com/ws/controller/function
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
i'm looking to add another redirect, just the same, for all request that contain /email/ so www.domain.com/email/controller/function will redirect to the /email/index.php.
my full .htaccess looks like the code below but it seems the /email/ never gets called.
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
RewriteEngine on
RewriteBase /email/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
I've tried adding ^/email to the RewriteRule conditional but to no avail.
Any help would be appreciated.
Thanks
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ws/(.*)$ ws/index.php?url=$1 [NC,L,QSA]
RewriteRule ^email/(.*)$ email/index.php?url=$1 [NC,L,QSA]

Change full url path with .htaccess

I want to change page url with .htaccess my url is 'page.com/web/' and I want to have just page.com. Could you give me htacces code. for example
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]
Try this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ web/$1 [NC,L]

htaccess redirect if URI contains string to index.php

I would like to redirect link http://example.com/asdasdas/edit to http://example.com/asdasdas/index.php/edit using htaccess. I did this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} edit
RewriteRule ^ %{REQUEST_URI}/index.php [L,R=301]
but it's a loop and doesn't works well. Any ideas?
You can use this rule in /asdasdas/.htaccess:
RewriteEngine On
RewriteBase /asdasdas/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php/$ [L]
This will support URL of http://example.com/asdasdas/edit

Using htaccess to pass directories to GET parameters Without rewriting url

I would like to pass (not redirect) something like this:
http://www.example.com/ (with / optional)
passed to the script
http://www.example.com/index.php
http://www.example.com/foo/ (with / optional)
passed to the script
http://www.example.com/index.php?nav=foo
http://www.example.com/foo/bar (with / optional)
passed to the script
http://www.example.com/index.php?nav=foo&subnav=bar
Furthermore I would like to pass the subdomain http://testumgebung.example.com to http://www.example.com/testumgebung.php with the same parameters as above.
I just managed to do the following, but it rewrites the url in the browser bar to the passed location and doesn't leave the url as before:
RewriteCond %{HTTP_HOST} ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [R,L]
You want to get rid of the R in the last 2 rules:
RewriteCond %{HTTP_HOST} ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [L]
The R in the square brackets tell mod_rewrite to redirect the browser, which changes the URL in the browser's location bar.
.
I had to change the order to achive what I wanted
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ ?nav=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ ?nav=$1&subnav=$2 [R,L]
RewriteCond %{HTTP_HOST} ^testumgebung\. [NC]
RewriteRule ^$ testumgebung.html [L]
and to rewrite the html so that the external resources start with a slash.

.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