Having trouble rewriting php file with .htaccess - php

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]

Related

Creating .htaccess hidden redirect

How can I make redirect from /katalog/protein/api and /katalog/protein/activelab to katalog/protein/?ms|manufacturer=API and katalog/protein/?ms|manufacturer=ActiveLab.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/katalog/(protein|gainer)/(api|activelab)/?$ /katalog/$1/?ms|manufacturer=$2 [L,QSA]
Your code look fine, just remove the leading slash from RewriteRule's pattern.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^katalog/(protein|gainer)/(api|activelab)/?$ /katalog/$1/?ms|manufacturer=$2 [L,QSA,NE,NC]

Trailing slash url .htacces

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]

Change URL name with mod rewrite on htaccess

I am having a tough time to understand how mod_rewrite works.
I have a url like this:
www.example.com/store/index.php?categ=headbands
And I want to make it look like this:
www.example.com/store/headbands
I am trying this, but not working:
RewriteEngine on
RewriteBase /store/
RewriteRule ^index/([^/\.]+)/?$ $1 [L]
And I confirmed the mod_rewrite is activated with a random example I found.
Any suggestions?
Thanks!
Try This one
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/store/(.*)$ /store/index.php?categ=$1 [NC,L]
OR
Put the .htaccess file inside the "store" directory with following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?categ=$1 [NC,L]

Why my htaccess rules do not work perfectly?

I want to make 2 types of URL with these styles using htaccess and mod_rewrite.
http://www.site.com/product
http://www.site.com/product/1/something
For this, I've added these lines to the htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)$ /index.php?p=$1 [F]
But whenever I run my application and hint http://localhost/project/, web server redirect me to http://localhost/.
Now I have 2 questions:
Why do I redirect to localhost and why the codes above do not work ?
How can I add a trailing slash to the end of clean URLs?
Adding
RewriteBase /
should help
Change the rules to
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [F]
The final .htaccess should look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
You probably want to get rid of the N and F flags. Not sure why you are getting redirected, it doesn't look like the rules you have are responsible for that. You also want to repeat the 2 conditions you have for both rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L,QSA]

Write a php code to be seo permalink url according to current htaccess

Below is example a famous .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What I know here, the result for URL will be
http://domain.com/about.php will be http://domain.com/about/
http://domain.com/contact.php will be http://domain.com/contact/
Example my URL is http://domain.com/page.php?id=1
How to write a code in PHP which the URL will be http://domain.com/page-name/ according to the .htaccess above.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]*)/$ /page.php?name=$1 [L]
this is what you search for
http://domain.com/testname/ will be translated to http://domain.com/page.php?name=testname

Categories