htaccess: Rewrite URL issue for query string in parameter - php

I having issue for re-writing url from
http://localhost/cs/compare?slug=wash-safe-industries.html
to
http://localhost/cs/compare/wash-safe-industries.html
I have tried following code in .htaccess but does not work for me, I have also searched various sites but still facing issues
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?compare/(.*?)/?$ /compare.php?slug=$1 [L]
Also tried following line of code
RewriteRule ^cs/(.*)$ cs/compare.php?slug=$1 [L,QSA]
Please help me out thanks

Try this .htaccess in /cs/ sub-directory:
Options -MultiViews
RewriteEngine on
RewriteBase /cs/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^compare/(.+?)/?$ compare.php?slug=$1 [L,NC,QSA]

Try using following line of code:
Options -MultiViews
RewriteEngine On
RewriteRule ^compare/(.+?)/?$ compare.php?slug=$1 [L,NC,QSA]

You have to write as follows, try this one.
RewriteRule ^compare/wash-safe-industries.html compare?slug=wash-safe-industries.html

Related

How to write multiple .htaccess rewrite rules?

I have multiple php pages like this:
http://www.example.com/si/article.php?id=122&nTitle=my-title
http://www.example.com/si/post.php?id=352&pTitle=my-post-title
i would like to rewrite them to
http://www.example.com/si/122/my-title
http://www.example.com/si/352/my-post-title
I tried different things in my .htaccess file and no result. Please tell me how to make to work with multiple rules. If I use one rule in .htaccess file its work for one php page like below:
RewriteRule ^([\s\w-]+)/(.*)/?$ article.php?id=$1&nTitle=$2 [L,QSA,NC]
This is now I tried it for multiple pages:
<ifModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /article.php?id=$1&pName=$2 [L,QSA,NC]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /post.php?id=$1&pName=$2 [L,QSA,NC]
</IfModule>
Can anyone give me an advice on how to use this functionalities in .htaccess?
Thank you.
You can use url like : (look like different from each other)
http://www.example.com/arical-si/122/my-title
http://www.example.com/post-si/352/my-post-title
then redirect to :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$0
RewriteRule ^artical-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ article.php?id=$1&nTitle=$2 [L]
RewriteRule ^post-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ post.php?id=$1&nTitle=$2 [L]
</IfModule>
First of all you should test your regex, you can do it there regex generator
Then in your htaccess, you can put your regex like this:
RewriteEngine on
RewriteRule ^/post.php?id=([0-9])&pName=([a-z]+)$ post/$1/$2 [L]
RewriteRule ^/article.php?id=([0-9])&pName=([a-z]+)$ article/$1/$2 [L]
try it step by step and you can debug it easily.

.htaccess php get error

I'm new to the .htaccess thing and did a couple of routing which worked fine but i just realised that my $_GET[] is being discarded. please how do i fix this.
Below is my PHP code:
<?php echo $_GET['er']?>
and my .htaccess code:
#turn rewrite engine on
RewriteEngine on
Rewritecond %{REQUEST_FILENAME} !-d
Rewritecond %{REQUEST_FILENAME} !-f
RewriteRule ^signup/([0-9]+)$ -/register.php?er=/$1 [QSA,L]
Any help is much appreciated.
Thanks in advance.
Try this
RewriteRule ^signup/([0-9]+)$ -/register.php [QSA,L]
so when the request
http:.../register.php?er=test;
You can now $_GET["er"] from within your php file.
an example that worked for me
RewriteEngine on
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

I am using htaccess that is not working

I have url like
http://example.com/index.php/allmodels/Samsung-mobiles_80
and i want to convert into
http://example.com/index.php/allmodels?st=Samsung-mobiles_80
I am using following code
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^allmodels/(.*)$ /allmodels?st=$1 [NC,L]
but this code is not working.
All the setting is correct I have tested and htaccess is working.
When i used following code that is working but above given code is not working.
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [NC,L]

Clean url mod_rewrite not working

Currently I have a url
www.mysite.com/folder/details.php?d=sample.com
and I want it to change like this one
www.mysite.com/folder/sample.com
This is my .htaccess file, but it is not working:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ details.php?d=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ details.php?d=$1
I also tried this one, but its not working also:
RewriteEngine On
RewriteRule folder/([0-9]+) details.php?d=$1
This is my url link php code:
<?php echo $var['var']; ?>
I tried some of the answers I found in this site but still I cannot figured out what I am doing wrong. Anyone?
Thanks!
You need some conditions to prevent looping:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_.-]+)/?$ details.php?d=$1 [L]
You can also combine the two rules into one by using the /? at the end to make the slash optional.
For the rules to be placed in the document root, you need to add the folder names to the pattern and the target:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/([a-zA-Z0-9_.-]+)/?$ /folder/details.php?d=$1 [L]
Thanks for your comments #Jon Lin and #quasivivo , I found a solution to my problem, this is my htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^portfolio/(.*)$ /portfolio/details.php?d=$1 [L,QSA,NC]
and I also change my url link code:
<?php echo $var['var']; ?>
Hope this will help someone in the future.

URL doesn't work with slash after removing extension using htaccess

I have been working on localhost, and my htaccess file is
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
after adding the htacces code,the url
localhost/movies/news.php
works
localhost/movies/news
also works but
localhost/movies/news/
doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.
You an try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The problem is when you add the slash you have news/.php and this is not working.
A better solution is to rewrite to a GET variable something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]
Then you can filter the GET variable in your script and include the file or content you need.

Categories