How to write multiple .htaccess rewrite rules? - php

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.

Related

htaccess adding .php to the end of $_GET variables when adding slash

I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.

Rewrite URLs in htaccess for multiple pages

I need to redirect urls from two different pages to readable URLS.
my .htaccess looks like this
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ page.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ page.php?pid=$1
The first urls using the blog-detail.php work fine. My URLS can be like mysite.com/latest-news-story
However, the second with page.php does not work unless I remove the blog-detail rewrite rules. How can I have both?
You can actually combine your rules that check for terminating / into one.
So, you actually just need the following two rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/-]+)/?$ page.php?pid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ blog-detail.php?url=$1 [QSA,L]

Htaccess redirect not successfully working

I am currently creating a site in localhost and have no luck in getting redirect working. I want to redirect
http://localhost/s/profile.php?uid=16
to
http://localhost/s/16
How could I go about doing this. I have tried using examples online with no luck.
Edit: I tried
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /s/profile.php?uid=$1 [L]
Using copy and paste from online. I always try to figure out who to use htaccess but can't seem to do so.
Here is the base functionality you are looking for. This is just a simple example and you should certainly do your research on this subject.
Alternatively, there are plenty of generators.
http://www.htaccessredirect.net/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile\.php$ - [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /profile.php [L,NE]
</IfModule>
You can have this rule
RewriteRule ^s/([^/]*)/?$ /s/profile.php?uid=$1 [L]
In the htaccess file in your "s" folder, try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+s/profile\.php\?uid=([^&\ ]+)
RewriteRule ^ /s/%1.php? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)\.php$ /s/profile.php?uid=$1 [L]

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

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