get page no from url using htaccess - php

I want to get page no from url
Eg:
www.example.com/education/tutorial/php-234.html
to
PageNo=234
and
www.example.com/course-1504.html
to
PageNo=1504
I am using the following code in .htaccess
RewriteEngine on
RewriteRule ^([0-9]+).html$ Pages.php?PageNo=$1 [NC,L]
But it is not working
If we use
RewriteRule ([0-9]+).html$ Pages.php?PageNo=$1 [NC,L]
I got page no, But
RewriteRule ^([a-zA-Z0-9-]+).html$ Pages.php?PageName=$1 [NC,L]
is not working
Any suggestion for both combination

try:
RewriteEngine on
RewriteRule ^(.*)-([0-9]+)\.html$ Pages.php?PageName=$1&PageNo=$2 [L,QSA]

Related

Different htaccess directory and subdirectory redirect

I am trying to have clean urls on my site and here is the mapping I'm trying to achieve:
mysite.tld/directory/ is going to > mysite.tld/page.php?q=directory
mysite.tld/tags/directory/ is going to > mysite.tld/tag.php?q=directory
I have made an htaccess file at the root of my site wiht the folling code:
RewriteEngine On
RewriteRule ^([^\/]+)\/?$ pages.php?q=$1 [L,QSA]
RewriteRule ^tags/([^/d]+)/?$ tags.php?q=$1 [L,QSA]
For some reason, the tag redirect works but is then "eaten" by the other rule of redirection. Not sure what to do to prevent second rule from doing this.
Answer is as follow:
RewriteEngine On
RewriteRule ^tags/([^/d]+)\/? /tags.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/tags
RewriteRule (.*) pages.php?q=$1 [L,QSA]

.htaccess URL rewriting not working on pagination?

I have this following url localhost/test/index.php?page=1 And i want it to be
'index.php/page/1'
so far I have this :
RewriteEngine On
RewriteRule ^page/([0-9]+)/?$ index.php?page=$1 [NC,L]
But it's not working .Any suggestion what am i doing wrong ?
Try with this
RewriteRule ^page/([^/]*)$ index.php/?page=$1 [L]
If you set it correct then URL like this http://localhost/test/page/1 should work
Try this one
RewriteEngine on
RewriteRule ^(.*)/page/([0-9]+)$ /index.php?page=$2 [NC,L]
The url will be
localhost/test/index.php/page/123
If you want the url looks like
localhost/test/index.php/page/123/
change you rule into this one
RewriteEngine on
RewriteRule ^(.*)/page/([0-9]+)/$ /index.php?page=$2 [NC,L]
RewriteRule ^index.php\/page\/(.*?)\/?$ index.php?page=$1 [NC]
The above code should solve your probleem. But you may want to read up on mod_url_rewrite
See https://www.sitepoint.com/guide-url-rewriting for more info

.htaccess redirect with Multiple query parameter

I would like to use following URL for my web site.
http://mywebsite.com/product.php
to
http://mywebsite.com/product
http://mywebsite.com/product.php?id=5
to
http://mywebsite.com/product/5
http://mywebsite.com/product.php?action=delete&id=5
to
http://mywebsite.com/product/delete/5
http://mywebsite.com/product.php?action=edit&id=3
to
http://mywebsite.com/product/edit/3
I use this code on my .htaccess file
RewriteEngine on
RewriteRule ^product product.php
RewriteRule ^product/([a-zA-Z0-9_-]+)$ product.php?id=$1
RewriteRule ^product/([a-zA-Z0-9_-]+)/$ product.php?id=$1
But problem is when i use http://mywebsite.com/product/5 and display with GET variable,
it does not pass the id parameter and how can i achieve the multiple query from my url?
Try this htaccess :
RewriteEngine on
RewriteRule ^product/?$ product.php [L]
RewriteRule ^product/([a-zA-Z0-9_-]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/delete/([a-zA-Z0-9_-]+)/?$ product.php?action=delete&id=$1 [NC,L]
RewriteRule ^product/edit/([a-zA-Z0-9_-]+)/?$ product.php?action=edit&id=$1 [NC,L]

Friendly URLs in PHP

This is the URL path I currently use:
/index.php?pid=211
I want to get the URL path as
/index/211/title of the product
How to do user friendly URLs in PHP?
And how to get the value of the "title"?
Regards,
In you html code you must write like this:
<a href='http://www.domain.com/index/211/title-goes-here'>url_text</a>
and in the htaccess file adapt the following:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]
RewriteRule ^(member)-([0-9-]+)$ index.php?action=member&id=$2 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)-([0-9-]+)$ index.php?action=$1&saction=$2&sid=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([0-9-]+)$ index.php?action=$1&id=$2 [NC,L]
RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/(.*).html$ index.php?action=details&id=$1&p1=$2&p2=$3&p3=$4 [NC,L]

.htaccess short and friendly url

I'm looking for the right .htaccess rule to make a Facebook.com/user URL like
What I want if I access for example
www.domain.com/StackOverFlow
It will be rewrite to
www.domain.com/index.php?category=StackOverFlow
Or
www.domain.com/category.php?item=StackOverFlow
Any help? I've tried some question here, but they doesn't explicit do this behavior.
Thanks in advance
Edited:
Forgot to say that I only want to be redirected using this rule, anything that after the domain, doesn't have any extension
For example it should redirect www.domain.com/categoryDescription but no www.domain.com/categoryDescription.php or www.domain.com/categoryDescription.html
The example below takes the URI after .com/ and assigns that to $1.
You can do the same for "item"
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?category=$1 [L]
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?category=x'.
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
</IfModule>
Try:
RewriteRule ^StackOverFlow$ index.php?category=StackOverFlow [NC,L]
or:
RewriteRule ^StackOverFlow$ category.php?item=StackOverFlow [NC,L]
In generally:
RewriteRule ^([^/]*)$ index.php?category=$1 [NC,L]
or:
RewriteRule ^([^/]*)$ category.php?item=$1 [NC,L]

Categories