php user and SEO friendly URL - php

I'm trying to change my default url with its parameter from
localhost/webiste/posts.php?name=what-is-btc
to
localhost/webiste/posts.php/what-is-btc
I've tired to main htaccess code RewriteRule ^post([A-Za-z0-9-]+)/?$ post?name=$1 [NC]
But it's not working, can anyone please help me find a way to get it to work?
NB. I'm calling my data dynamically from database and I'm using php get to the name.
Thanks in advance.

You're missing the .php in your RewriteRule and also a leading slash.
Try this:
RewriteRule ^post/([A-Za-z0-9-]+?)/?$ /post.php?name=$1 [NC, L]

Related

Search replace query string with htaccess

I have my site url like this.
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&twdg_wsac=1
I want to replace some part of this url using htaccess so the final output should be like this
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=1
So I have made changes in the htaccess file like this
RewriteEngine on
RewriteRule ^&twdg_wsac$ &custom_posts
But its not working. Also as you can see in the url there is twdg_wsac=1. So the last "1" is a pagination for post so that would change dynamically as per posts count.
So can someone tell me how to do this?
Any help and suggestions would be really helpful. Thanks
Update
All the parameters in the url are dynamically generated I mean those are the filter parameters. So they cant be same except twdg_wsac
Try with below, we are doing rewrite on query string.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^orderby=(.+)&s=(.+)&post_type=(.+)&twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=%1&s=%2&post_type=%3&custom_posts=%4
RewriteEngine On
RewriteCond %{QUERY_STRING} twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=%1 [L]

Arrangement, Skipping and URL re=writing

I tried to re-write on my own but unable to do so, basically, I want to re-write php-based URL with .htaccess but also need to skip few parameters of php URL for example;
Original URL:
http://example.com/details.php?id=62?title=billions-s01e09-webrip-x264-fumettv
Required Format:
http://example.com/billions-s01e09-webrip-x264-fumettv-id62.html
Format Sequence:
MySiteURL/Title-of-Post-PostID.html
Looking for kind response, please guide me how can i make this possible through .htaccess.
waiting for kind response...
Good day
As you already know that you have to change the .htaccess file in your server. You can use regx to achieve your required url. Using the following code can help you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)-id([0-9]+).html?$ details.php?id=$2&title=$1 [QSA,L]
worked fine for me. Hope will work for you too.
To rewrite
http://example.com/foobar-id123.html
to
http://example.com/details.php?id=123?title=foobar
You can use the following rule :
RewriteEngine on
RewriteRule ^([^-]+)-id([0-9]+)\.html$ /details.php?id=$2?title=$1 [NC,L]

Rewriting url in apache is not working

I am using the following code in .htaccess direct editor in ipage server for changing my website's page url from
for example:
http://foodinger.in/viewRestaurant.php?raipur=Barbecue-Country&id=3006
to
http://foodinger.in/viewRestaurant/raipur/Barbecue-Country
but it is not working
RewriteEngine on
RewriteRule viewRestaurant/raipur/(.*)/ viewRestaurant.php?raipur=$1&id=$2
RewriteRule viewRestaurant/raipur/(.*) viewRestaurant.php?raipur=$1&id=$2
Am i doing anything wrong, Please anyone suggest me the right way if i am wrong ?
Thanks in advance
You specify only one group for substitution (.*) that will replace $1 in target url.
Try:
RewriteEngine on
RewriteRule "^/viewRestaurant/raipur/(.*)/(.*)/?" "/viewRestaurant.php?raipur=$1&id=$2"
And query with Id: http://foodinger.in/viewRestaurant/raipur/Barbecue-Country/3006

modify URL using .htaccess

I want to change url for example http://www.mysite.com/cgshop/admin/index.php?route=common/home will become http://www.mysite.com/cgshop/cp/index.php?route=common/home.
Please help me to figure out this. Thanks in advance.
This should get you what you want.
It tests to see if the URI begins with /cgshop/admin/ and then captures the rest so it can redirect with a 301 to the new url. The QSA means it will also carry over the query string (everything after the ?).
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgshop/admin/.*$
RewriteRule ^cgshop/admin/(.*)$ /cgshop/cp/$1 [R=301,L,QSA]
RewriteEngine On
RewriteRule ^cgshop/cp/.*$ cgshop/admin/$1
Try this.......

Apache Rewrite rule confusion

I'm trying to convert a simple url (below) in to a blog-style url, but not quite sure how to do it, all of my other rules are working fine, but I can't seem to figure this one out.
URL I want to convert: http://www.website.com/myblog.php?id=1&title=My+blog+title
URL I want it to create: http://www.website.com/1/my-blog-title
What should the rule be?
Any assistance appreciated :)
Try this
RewriteEngine on
RewriteBase /
RewriteRule ([0-9]+)/([^.]+) myblog.php?id=$1&title=$2
Try this in your .htaccess file:
RewriteEngine on
RewriteRule ^(\d+)/([^/]+)$ myblog.php?id=$1&title=$2
But here the hyphens are not replaced by plus signs.
in your .htaccess file,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /myblog.php?id=$1 [L]
You don't (well shouldn't) need to pass the blog title to the blog file, only the ID. Hope this works

Categories