Changing the URL pattern in PHP - php

Right now my URL looks like:
https://www.example.com/blogitem.php?id=29
What I want is the user will see the URL in their browser like:
https://www.example.com/blogitem/the-title-of-the-current-blog
This thing is already implemented in Wordpress and other CMS but I am using pure PHP and don't have any idea about how can I implement this feature. Can anyone please help me to implement this?

write this on code .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?id=$1 [L,QSA]
you need to get title form database instead of id
so it will be like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?title=$1 [L,QSA]

Related

How can I rewrite a url by keeping the file name as it is but the parameter being passed should be the folder name?

I am passing a name which will be used as the subfolder but the filename remains the same eg foldername/gallery.php?q=new_folder should be new_folder/gallery.php using htaccess.
Attaching the htaccess of what I a trying to do
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(gallery.php)$ $2.php?q=$1 [L]
gallery.php is the filename and $1 is basically the new_folder. Been struggling with this for quite a bit.
You are fairly close. You just need to use foldername/ in target URI of your rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(gallery\.php)$ foldername/$2.php?q=$1 [L,QSA,NC]
I have just added bellow mentioned code in .htaccess. Hope this will help you.
RewriteEngine On
RewriteRule ^foldername/(.*) http://example.com/newfolder/gallery.php?options=$1 [R=301,L]

Removing '&title' from the URL in .htaccess

I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]

Custom links with .htaccess file to access two parameters or more

I am working on XAMPP V.3.2.1 on Windows 8.1 64-bit.
I have the following code in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /?cmd=$1 [NC,L]
Now, when i write link like
http://localhost/aboutUs
http://localhost/MyAnyOtherPage
it works fine even if i use form in the page with post back method, but when i try to use links with two parameters and i change my .htaccess file link like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /?cmd=$1&caseSno=$2 [NC,L]
In this way i am accessing my page by cmd and searching database record with caseSno parameters.
To achieve this how can i write my hard coded links to run my page with searching database tables and pitch records with second parameter caseSno?
This is not working for me:
My Original link is :
localhost/?cmd=caseDetailsByCaseSno&caseSno=2308
How to write this?
The following is not working for me?
http://localhost/caseInformation/case/234
Thanks in advance
This should help you with two parameters.
If you have this URL http://localhost/caseDetailsByCaseSno/234 then you should be able to use this htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?cmd=$1&caseSno=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /?cmd=$1 [NC,L]

Masking the special pages in mediawiki

We are developing a application using mediawiki. we are not fully aware of mediawiki. We have created a special page as mediawiki/Special:Video. But we do not like to view the word Special: in our url. i.e., we would like to view the url as mediawiki/Video instead of mediawiki/Special:Video. If are not aware of mediawiki functions fully. So kindly let us know how to do this url masking in mediawiki. I have tried like this
Options +FollowSymLinks
RewriteRule ^ajax.php($|/) - [L]
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^mediawiki/video$ mediawiki/Special:Video
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(/?[^/]*)$ /mediawiki/index.php?title=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /mediawiki/index.php?title=$1&actions=$2 [L]
But it is not working
You can rewrite it as follows, no need to use RequestCond
RewriteRule ^mediawiki/Video$ /mediawiki/Special:Video

mod_rewrite: multi-level URL using a request parameter

I currently have this set up and working fine inside a users folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1
e.g 127.0.0.1/site/users/admin goes to 127.0.0.1/site/users/index.php?i=admin
Now as this is for a profile page, how would i do something such as this.
users/admin/activity
So that it would show the activity page for that user? I am totally confused on how i would go about this.
Would it be best to make the index.php accept a page $_GET variable? But the how would i get htaccess to work around it?
You rules will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?i=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1 [L]
Now your index.php should be getting 2 $_GET variables, i and page.

Categories