Trailing slash url .htacces - php

i was confused about trailing slash, this is script i got from internet
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
Options All -indexes
My question:
1.what function of %{REQUEST_FILENAME} !-f
2.what function of RewriteCond %{REQUEST_URI} !index.php
3.what function of RewriteCond %{REQUEST_URI} !(.*)/$
4.how can i write Rewrite url if the original url like:
def.php?p=cpanel&m=add_user ,
i want the link above rewrite like cpanel/add_user
thanks

The rule need is this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cpanel/([^/]+)/?$ def.php?p=cpanel&m=$1 [L,QSA,NC]
Reference: Apache mod_rewrite Introduction
RewriteCond %{REQUEST_FILENAME} !-f means iif request is not for a valid file
$1 is backreference to captured string after /cpanel/
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
NC is for ignore case
L is for Last rule

1.what function of %{REQUEST_FILENAME} !-f
2.what function of RewriteCond %{REQUEST_URI} !index.php
3.what function of RewriteCond %{REQUEST_URI} !(.*)/$
You should read the rewrite guide http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
how can i write Rewrite url if the original url like:
def.php?p=cpanel&m=add_user ,
i want the link above rewrite like cpanel/add_user
You can use this code in you .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cpanel/(.*)$ def.php?p=cpanel&m=$1 [L,QSA,NC]

Related

Mod Rewrite lost all GET parameters [PHP]

Today i start with my mod rewrite for the php script.
Some words to the setup
I use a subdomain e.g. test.testsystem.com which points to root/test/
So i have some multiple GET Parameters for my PHP script like the following:
1) https://test.testsystem.com/index.php?a=foo
2) https://test.testsystem.com/index.php?a=foo&v=bar
3) https://test.testsystem.com/index.php?a=foo&c=testcode
so with mod rewrite i want to have
1) https://test.testsystem.com/foo
2) https://test.testsystem.com/foo/bar
3) https://test.testsystem.com/foo/c/testcode
What i currently have is
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /test/
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1 [QSA]
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1&v=$2 [QSA]
For the first two URLs. Now it does no error 500 any more but the problem is i do not get any GET parameters in my php script.
Maybe someone can provide me here a working solution.
Regards
Assuming your test.domain.com is pointing to the /test folder , you can use something like the following in your /test/.htaccess .
Options -Multiviews
RewriteEngine on
#1)Rewrite /foo to /index.php?a=foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?a=$1 [L]
#2)rewrite /foo/bar to /index.php?a=foo&v=bar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2 [L]
#3)rewrite /foo/bar/buzz to /index.php?a=foo&v=bar&z=buzz
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2&z=$3 [L]
The RewriteConds are important in each rules to avoid rewriting your existing files and directories .

Having trouble rewriting php file with .htaccess

So I have a URL that is domain.com/s_cart.php?&pid=1 and /s_cart.php?&gid=2
How would i go about setting a .htaccess to rewrite s_cart.php to the following
domain.com/order/&pid=1
Tried the following
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/order([/]*)$ ./s_cart.php?gid=5 [L,NC]
Didn't work.
Basically I'm trying to add domain.com/order?pid=1
When the actual url currently is domain.com/s_cart.php?pid=1
Try this :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^order\.php$ /s_cart.php [L,QSA,NE]
Match only against URI and query string will be appended as it is with QSA flag.
If you want to match pid only in query string , replace like this :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^&pid=
RewriteRule ^order\.php$ /s_cart.php [L,QSA,NE]

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

mod_rewrite removing name of query string

I have big problem with my urls. I know how to remove index.php form url, but problem is that I don't know to remove that "?act=" from url.
My link now is http: //localhost/doctrine/public/?act=test and I want to create it like http: //localhost/doctrine/public/test.
My current .htaccess file placed in public folder:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I need help how to do that.
Use this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /doctrine/
RewriteRule ^(public)/([^/]+)/?$ $1/?act=$2 [L,QSA,NC]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Before your expression engine rule, you can add:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /doctrine/public/?act=$1 [L]
To internally rewrite a request like http: //localhost/doctrine/public/test to /doctrine/public/?act=test. You need to make sure the links in your content look like /doctrine/public/test instead of the one with the query string. If there are links outside of your control (like google indexed pages) you can add this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /doctrine/public/?\?act=([^&\ ]+)
Rewrite Rule ^ /doctrine/public/%1? [L,R=301]
to point browsers to the URL without the query string.
This unfortunately isn't going to be compatible with your expression engine URLs.

Mod Rewrite and PHP

I am trying to redirect pages using mod_rewrite to the pages with some variables (for using them with PHP's $_GET).
To give an example with few lines:
When the user enters /c/stg it redirects to item_show.php?id=$1&f=1 (where f is page number).
When the user enters /c/stg/2 it redirects to the second page with show.php?id=$1&f=$2.
I think there are no errors or misuses of these in my file but, here's what I want:
I want user to enter /string directly to go item_show.php?id=$1&f=1 with $1 is our string of course...
But when I change my file by removing the /c/ part from RewriteRule it starts giving errors in all other directories and doesn't read any files (my.css) even though I have already defined a RewriteCond %{REQUEST_FILENAME} !-f...
Do you have any suggestions?
Or how can I made this system possible with any method?
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteRule ^c/([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Am I wrong if RewriteCond only are passed on to the first RewriteRule??
Try this instead:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Cheers,
RewriteCond directives do only belong to the first following RewriteRule directive. So in your case the two RewriteCond direcitves are only applied to the first RewriteRule directive and not to the others.
But you could use this abortion rule to quit the rewrite process if the request can be mapped to an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This would cause that following rules are only tested if the request cannot be mapped directly to an existing file or directory.

Categories