Friendly URL with two properties? - php

I am trying to rewrite an ugly URL that's using two properties. For example, I want example.com/stories/?url=how-are-you?num=5 to be example.com/stories/how-are-you/5 I have rewritten the first property, but I don't know how to rewrite the num property. Here's my .htaccess file:
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]

Try :
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+)&num=([0-9]+) [NC]
RewriteRule ^ %1/%2? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&num=$2 [L,QSA]

Related

Ignore a particular folder in htaccess

I am facing issues regarding my .htaccess. The .htaccess works, but it causes a problem with the admin folder. Would it be possible to exclude that specific folder from this .htaccess? Here is my .htaccess. It is outside the admin folder.
Options -MultiViews
ErrorDocument 404 http://localhost/blo-prt/err.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blo/([^/\.]+)?$ blo-det.php?postlist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/blo/([^/\.]+)?$ blo-prior.php?bloglist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tep/([^/\.]+)?$ temp-det.php?telist=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/tep/([^/\.]+)?$ temp-of-prior.php?tlist=$1 [L,QSA]
You should add this to your RewriteConds:
# if request is not for the /admin/
RewriteCond %{REQUEST_URI} !^/admin/ [NC]
Personally, I use this code to remove the .php extension:
# enable url rewriting
RewriteEngine On
# remove .php
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Also, you should only type RewriteEngineOn just once at the top of the file.

.htaccess mod_rewrite on multiple PHP files issue

I'm trying to beautify URL's for SEO from http://example.net/dashboard.php?action=news to http://example.net/news
And it's working with this code
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
But I want to edit more PHP files, for example, http://example.net/user.php?action=profile to http://example.net/profile
If I use this code again in the same .htaccess file
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ user.php?action=$1 [L,QSA]
It won't work, only dashboard.php one is working properly.
Can you please help me with this?
Thank you in advance.
Your current Rewriterules are to general.
[^/]+
matches everything which has no "/", else its matching everything.
So basicly your first rule
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
matches everytime. profile/ will always redirected to dashboard.php?acton=profile.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ dashboard.php?action=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/?$ user.php?action=$1 [L,QSA]
As you can see i switched
[^/]+
with more explicit regex.

Getting value with htaccess and pagination for root

This url
example.com/videos/load.php?cat=video-category
would become
example.com/videos/video-category
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&p=2&limit=20
it would become
example.com/videos/video-category?p=2&limit=20
(this links also work)
example.com/videos/video-category?p=2
example.com/videos/video-category?limit=20
Also, there is another root url
example.com/videos/load.php?cat=video-category&name=video-name
it would become
example.com/videos/video-category/video-name
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&name=video-name&p=2&limit=20
And it would become
example.com/videos/video-category/video-name?p=2&limit=20
(this links also work)
example.com/videos/video-category/video-name?p=2
example.com/videos/video-category/video-name?limit=20
Here is my htaccess (into videos folder)
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \ /+videos/load\.php\?cat=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ %1?%2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]
how to do this.. plz help me.. thanks..
Have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&name=([^&\s]+)&?(\S*)
RewriteRule ^ %1/%2?%3 [L,R=302]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&?(\S*)
RewriteRule ^ %1?%2 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ load.php?cat=$1&name=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]

url rewriting while url creates dynamically

I had rewrite a URL from example.com/service.php?p_id=1&s_id=seo to example.com/1/seo/.
By The following rule:
this rule is for example.com/services.php?p_id=1&s_id=seo
RewriteEngine On
RewriteCond %{THE_REQUEST} /services\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ services.php?p_id=$1&s_id=$2 [QSA,L]
Now the problem is that when i use the following rule for another url like example.com/about-us.php?p_id=1&s_id=2 and for example.com/it-training.php?p_id=1&s_id=2 then the url redirects as example.com/1/2/index.php but it should be as example.com/1/2 ,
formated rule for about-us.php is given bellow
RewriteEngine On
RewriteCond %{THE_REQUEST} /about-us\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ about-us.php?p_id=$1&s_id=$2 [QSA,L]
In other case i have some url like example.com/packages.php, index.php, i also want to remove the .php from that kind of files

htaccess rewrite rule to index.php?p=pagename

I am using this htaccess code to create a rewrite rule
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ index.php?p=$1.php [L,QSA]
i want to rewrite pages like
http://my.domain.net/index.php?p=tickets/openticket to look like http://my.domain.net/tickets/openticket
but its just showing index.php without the ?p=...
Try this code as your first rules:
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
try this.

Categories