URL Rewriting Using .htaccess for PHP - apache

I want to convert this URL example.com/post.php?id=12345&title=xyz to example.com/12345/xyz.
I am able to do example.com/12345 but I can't get /xyz.
RewriteEngine On
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?id=$1
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?id=$1

With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
This solution assumes that you are hitting example.com/post.php?id=12345&title=xyz sample url in browser and want to change it to example.com/12345/xyz
##Enabling engine here.
RewriteEngine ON
##Extrenal redirect 301 to mentioned url by OP in question.
RewriteCond %{THE_REQUEST} \s/post\.php\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite to post.php with query strings.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ post.php?id=$1&title=$2 [QSA,L]

Related

Rewrite URL using .htaccess by replacing /? with?

How can I rewrite URL (i.e. remove last / after test) using .htaccess on php page from
from www.example.com/test/?sku=23456&qty=3 to www.example.com/test?sku=23456&qty=3
from www.example.com/page2/?page=3&emp=543 to www.example.com/page2?page=3&emp=543
from www.example.com/stream/?start=4&id=tdfcs45s&q=sat to www.example.com/stream?start=4&id=tdfcs45s&q=sat
I tried but it doesn't work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
With your shown samples and attempts please try following .htaccess rules file. We need to use THE_REQUEST variable here of apache. Check this if this helps you, also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/test)/(\?sku=(\d+)&qty=\d+)\s [NC]
RewriteRule ^ %1%2 [L]

RewriteRule in htaccess only work when I visit url directly

I have htaccess file where I am turning regurlar url example.com/result?from=Brampton&to=Calgary&submit= to Rides-From-Toronto-Calgary-submit seo friendly url. But problem is that I manually have to type url to visit seo friendly version of url, how can I achieve redirect using .htaccess ?
This is what I have so far
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^from [NC]
RewriteRule ^Rides-From-([^/]*)-([^/]*)-([^/]*)$ result?from=$1&to=$2&submit=$3 [QSA,NC,L,R=301]
any help is appreciated.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Also these Rules consider that your internal rewrite is happening to index.php file in case its something else file then please change its name to it in rules.
RewriteEngine ON
##External redirect rules.
RewriteCond %{THE_REQUEST} \s/result\.php\?from=([^&]*)&to=([^&]*)&submit=(\S+)\s [NC]
RewriteRule ^ /Rides-From-%1-%2-%3=%3? [R=301,L]
##Internal rewrite rules from here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:.*?-){2}([^-]*)-([^-]*)-([^-]*)=(.+)/?$ result.php?from=$1&to=$2&$3=$3 [QSA,L]

issue with url parameters in htaccess

RewriteRule ^/pages/topic\.php?tag=$1 /topics/([^/]+) [R=301,L]
RewriteRule ^topics/([^/]+)/?$ /pages/topic.php?tag=$1 [END]
My problem is with the 301 redirect, I believe the problem is with the tag=$1 and /([^/]+) I'm not entirely sure I've done this correctly, My desired result is that when the user navigates to /pages/topic.php?tag=cryptocom that the user is then redirected to /topics/cryptocom/ Thanks for your help.
With your shown attempts/samples, please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/pages/([^.]*)\.php\?tag=(\S+)\s
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite rules here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^topics/([^/]+)/?$ /pages/topic.php?tag=$1 [NC,QSA,L]

.htaccess Rewrite Rule does not work correctly with index.php?path

Hi I have the following old url
www.domain.de/index.php?leistungen
and the new one is
www.domain.de/leistungen
I tried the following RewriteRuile like I did it millions of times.
RewriteRule ^index.php?leistungen /leistungen/ [L,R=301]
But in this case I got the following result:
www.domain.de/path/?leistungen=
And it it routes to the root url
What is the issue in this case?
Thanks in advance.
You may use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]

How to remove and avoid url directories in htaccess

I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]