Rewrite URLs in htaccess for multiple pages - php

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]

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 .

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.

Getting rid of GET keys parameters in the URL, keep only values

I wanna to get rid of GET string parameters, from keys.
For example this is initial uri:
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25&user_city=ohio
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25
http://example.com/api/get_users_method?user_city=ohio
I wanna make like this:
http://example.com/api/get_users_method/18/25/ohio
http://example.com/api/get_users_method/18/25
http://example.com/api/get_users_method/ohio
How can i get this result? I found some solution, to edit htaccess file, but it doesn't work unfortunately, maybe i made some mistakes in htacces file? I'm using php. Thanks!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by that rule.
You will 3 separate rules in your root .htaccess (a level above api directory)
Options -MultiViews
RewriteEngine On
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3&user_city=$4 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/?$ $1?user_city=$2 [NC,L,QSA]

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