Rewrite urls php/mysql page with htaccess apache - php

I have a search form to search for a health institution in a city.
when I display the results I have this URL for example :
search.php?city=mycity&speciality=cardiology
I would like to rewrite this URL like this: health-institution-cardiology-mycity
I set up an .htaccess rule like this :
RewriteEngine on
RewriteRule health-entity-([a-zA-Z\-]+)-([a-zA-Z\-]+) search.php?city=$2&speciality=$1
It doesn't work, however, I applied this rule to another type of URL and it works
is there any issue with the code?
do I need to add something?

This will work:
RewriteEngine on
RewriteRule ^health-institution-([a-z]+)-([a-z]+)/?$ search.php?city=$2&speciality=$1 [NC,L]
Note: The NC (No Case) at the end makes the rurl case insensitive.

Related

Unable to rewrite URL using Apache mod_rewrite in .htaccess

On the home page of my website, I have a guide that includes some links, and each redirects to a family guide page, all with the redirection by identification of each element of the guide, remaining as the link
https://example.com/family-guide/?id=4
Where '4' is the ID of the element.
I would like to leave like this:
https://example.com/family-guide/4
I tried to use mod_rewrite in the .htaccess file:
RewriteRule ^family-guide/([0-9] +)/([0-9a-zA-Z_-]+) family-guide/?Id=$1name=$2 [NC, L]
but nothing happens
Your rule has some spaces. Be careful because if there is a space between the flags [NC, L] the rule is not valid.
Another thing is that you want the url https://example.com/family-guide/4 to match a rule and your rule expects two parameters (id and name) while in this url there is only one.
I would use a couple of rules instead:
RewriteRule family-guide/([0-9]+)/([0-9a-zA-Z_-]+) family-guide/?id=$1&name=$2 [NC,L]
RewriteRule family-guide/([0-9]+) family-guide/?id=$1 [NC,L]
With this rules if you go to https://example.com/family-guide/4 you should be shown the content of https://example.com/family-guide/?id=4. And if you go to https://example.com/family-guide/4/whatever it will show you the content of https://example.com/family-guide/?id=4&name=whatever instead, which is what I understood you need.
Also, the rule order is important as if they were in the opposite order https://example.com/family-guide/4/whatever would match the other rule and show the content of https://example.com/family-guide/?id=4/whatever

URL structure change using .htaccess rule

I have no knowledge in regex and never done regex redirections.
htaccess
but... I am looking for a way to redirect this URL structure:
http://www.example.com/mypage/param1/
to this URL structure
http://www.example.com/mypage/?key1=param1
and, if possible, only if param1 contains # or %40
I saw this answer:
How to change URL structure with htaccess?
but that's the reveresed direction for my question
RewriteEngine on
RewriteCond $1 #
RewriteRule ^mypage/((?!index).+)$ /mypage/?key=$1 [NC,L,R]
Remove the R flag if you dont want the url to change.
This will redirect an url of the form :
/mypage/.*#.*
to
/mypage/?key=.*#.*

Rewrite dynamic url in .htaccess?

Here is an example url.
97006 is the query string.
Customer-Manager is a title that changes for every page.
jobdetails.php is run when a button is clicked which brings you to a page like this below.
http://11.11.111.111/97006/Customer-Manager.html
I would like the url to be:
http://11.11.111.111/job/Customer-Manager.html
In the .htaccess there is already code for the url I want to change.
I think this is the condition for the rule: (because its the 1st condition & 1st rule, is thst how it works?)
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
This is the rule I have tried changing many times but no success:
RewriteRule ^([1-9][0-9]*)/([-.,_0-9a-zA-z]*)\.html$ job_details.php?query_string=$1&action=%1 [L]
This code makes the url when for the button:
$button = tep_href_link($id.'/'.$title.'.html');
I have research apache directives, regular expressions and tried many tutorials but whenever I change anything in the htaccess for that line I get sent to the wrong url or the site crashes altogether.
I think it is impossible you can use this url instead :
http://example.com/jobs/97006/data.html
RewriteEngine On
RewriteRule ^jobs/([^/]*)/([^/]*)\.html$ /job_details.php?query_string=$1&action=$2 [L]

Custom url rewrite help needed

I have created a database with url slug and it contains urls like mysite.com/blog/content/user1 and blog is folder where file is so I want to change mysite.com/blog/page.php?name=content&user=user1 to mysite.com/blog/content/user1 how can I do that I have tried
RewriteEngine on
RewriteRule ^([a-z]+)$ page.php?name=$1&user=$2 but that doesn't works
Try this one
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([^//]+)/([^//]+)$ blog/page.php?name=$1&user=$2 [QSA,L]
Please avoid making the slug record using / which means the uri segment better to use - for you slug so if your url is something mysite.com/blog/page.php?name=content&user=user-a and you want to show like mysite.com/blog/content/user-a them above rule will also work for that url aso

URL Rewrite is not working properly

This is my URL rewrite:
RewriteRule ^cars/$ cars.php
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1
This works, so my URL is like this:
example.com/cars/porche/
refers to
cars.php?model=porche
Now Im making more search criterias, so I want to be able to add for example model year, car manufactor, etc. like this:
example.com/cars/porche/?model_year=xxx&car_manufactor=xxx
Right now this does not work with the current rewrite, but I can't figure out why.
Simple, just add QSA:
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1 [QSA]
That tells the rewrite engine to append any other query parameters to the rewritten URL as well.

Categories