how can write htaccess file to display user friendly url? - php

I developed one website. I want to display url for my website as below.
http://example.com/1/post
i am passing the query string as below
<a href="page.php?id=1&url=post>click me</a>
I wrote a below htaccess file to display above user friendly url
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.php\?id=([0-9]*)&url=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([0-9]*)/(.+)$ /page\.php\?id=$1&url=$2 [L]
But, when clicking Click me link, it load some time and display cannot reach message in browser. I dont no what is wrong in my htaccess code. Kindly help me to display url as i mentioned above. Thanks in advance.

Try this out
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/?$ page.php?id=$1&url=$2 [L,QSA]

Related

Changing the URL pattern in 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]

Rewrite URL for Profile Page

How do I use .htaccess to rewrite:
mysite.com/profile/[username]
go to:
mysite.com/profile.php?id=[username]
I don't want to have to reload the page just change the URL to make it look good.
Any help is appreciated.
This is the htaccess rule that should get you started:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule profile/(.+) profile.php?id=$1

How to rewrite an url via htaccess

I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]

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]

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