Rewrite Rules in Htaccess - php

New to htaccess: how can i write rewrite Rules for this,
instead of http://www.x.com/directory.php?state=TX have www.x.com/texas-fishing
instead of http://www.x.com/directory.php?state=WA have www.x.com/washington-fishing
Actually $1 indicates the dynamic part of the Url like(TX,WA..), but here i need complete title of the page. So do i need to modify in script or .htaccess is enough to manage.
If it is enough then how can we manage..

RewriteRule ^texas-fishing$ directory.php?state=TX [NC,L]
RewriteRule ^washington-fishing$ directory.php?state=WA [NC,L]

Related

Except some url from a rewrite rule

I inserted this rule in my .htaccess file:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
In this way I can get friendly urls like this:
http://localhost/mysite/london
I'd also like to use a friendly url for my contact page like so:
https://localhost/mysite/index.php?content=message to become:
https://localhost/mysite/contact
But if I insert the below rule into .htaccess...
RewriteRule ^contact/?$ index.php?content=message [NC,L]
...it doesn't work as it seems that the rule for the categories affects this rule.
In fact, if I comment out the category rule...
#RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
...the url friendly rule for the contact page works (https://localhost/mysite/contact)
So I'm looking for the possibility to exclude some parameter from the category rule to allow for a redirect in some case to another url.
Thanks for any suggestions...
Firstly you need to make sure the contact rule is placed before the more generic one so htaccess can process it first:
RewriteRule ^contact/?$ index.php?content=message [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
If you have it the other way around the generic rule will always be looked at and match before htaccess even gets to the contact rule.
Note though that the way you wrote your rules it will only match URLs such as
https://exmaple.com/contact
but not
https://exmaple.com/contact/something-else
However, looking at your more generic rule I assume this is intentional.

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

Not able to make seo friendly URL from php using htaccess

I have a problem in creating SEO friendly url actually I have two url rewritten one is working and other one is not. I dont know why? please help
My url is this
http://localhost/quotesnew/author.php?authID=1
and I want it to be
http://localhost/author.html
here is my code in .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /quotesnew/index.php?authchar=$1 [L]
RewriteRule ^author\.html$ /quotesnew/author.php?authID=1 [L]
You can try
RewriteEngine On
RewriteRule ^author/([^/]*)\.html$ /quotesnew/author.php?authID=$1 [L]
Now your url would be
http://localhost/author/1.html
The [L] flag stops rewriting for this cycle. It therefore never reaches the second rule.

how to retrieve a specific get data in htaccess and pass it on rewrite rule

I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.

.htaccess rewrite redirect

I have some code which uses Rewrite engine in my .htaccess file and it specifies the directory for my SERPs. However when someone goes onto search/QUERY/ rather than search/QUERY/PAGE/ it displays a 404 error. Same with just search/.
I want it so that if someone just goes to search/QUERY/ that it redirects them to search/QUERY/1/ and for just search/ it redirects them to my homepage /. I have included a copy of my code below.
RewriteEngine on
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&category=web&d=$2
Can anyone help me with this problem?
Thanks in advance, Callum
Try this code in your htaccess :
RewriteEngine on
RewriteRule ^search/?$ / [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search/$1/1/ [R=301,L]
RewriteRule ^search/([^/]+)/([0-9]+)/?$ search.php?q=$1&category=web&d=$2 [NC,L]
You can create multiple RewriteRule statements to accomplish this. (Make sure to omit the [R] flag on intermediate rules if you use it!)
The rule you posted will only rewrite if it's of the form /search/query/n/, so write a regular expression matching search/query that redirects to /search/query/1. Do the same to redirect home with no query.
Try these additional 2 rules in your .htaccess file:
RewriteRule ^search/([^/]+)/$ /search/$1/1/ [R=301,L]
RewriteRule ^search/?$ / [R=301,L]

Categories