mod_rewrite hide last part of url string - .htaccess

I have the following URL: www.mydomain.com/musician.php?musician=musicianname&id=idnumber
I am using mod_rewrite to achieve this: www.mydomain.com/musician/musicianname/idnumber
This is the rule I am using:
RewriteRule musician/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /musician.php?$musician=$1&id=$2 [L]
This is working just fine but I want to make this URL more user friendly. I would like to hide "musician" and "idnumber" from the url and have only "musicianname": www.mydomain.com/musicianname/
What is the best mod_rewrite rule to achieve this?

This is working just fine but I want to make this URL more user
friendly. I would like to hide "musician" and "idnumber" from the url
and have only "musicianname": www.mydomain.com/musicianname/
What is the best mod_rewrite rule to achieve this?
This doesn't really have anything to do with getting the best rewrite rule. That's not hard at all. In order to achieve what you want, you will first have to re-architect your PHP code. If you remove the idnumber from the query string, I presume it will not have it to look up the record in the database. So you will have to have your code lookup the data by musicianname instead.
Once you do that you can simply change your rewrite as such.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /musician.php?musician=$1 [L]

Related

.htaccess RewriteRule query isn't working

I can't get the following RewriteRule to work.
I have a PHP SQL query to display a web page. It requires a RewriteRule rule which I'm trying to achieve in a .htaccess file.
Here is the full URL at the moment.
www.example.com/category/sub-cat/page.php?art_url=a-page-of-mine
I can't get it to do
www.example.com/category/sub-cat/a-page-of-mine
My Code below:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /error-404.php
#error 404
RewriteRule ^error/?$ error-404.php [NC,L]
RewriteRule ^category/sub-cat/(0-9a-zA-Z]+) category/sub-cat/page.php?art_url=$1 [NC,L]
Can someone help me out?
AS I said in the comments
Missing a few things here (0-9a-zA-Z]+) like [- as in ([-0-9a-zA-Z]+)
This is going to bite you too...
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Match everything that doesn't have a dot and add .php to it with the [L] last flag. I would bet it will never pass that one in the first place.
Generally you want the more specific rules first, followed by the more generic ones last.
Also if I recall correctly the NC i no case, so you can get rid of the A-Z and just do [-a-z0-9]+
A better way
I try to avoid query string rewrites and rely on the URI method of rewriting common in MVC frameworks
example.com/index.php/category/sub-cat/a-page-of-mine
And then use a router and HTACCESS to only remove the index.php it's much simpler that way.
I have a pretty bare bones router on my GitHub page that shows how to route URL's like that.
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
One big issue with messing with the query string is you can lose the ability to use $_GET the way it's designed to be used for things like search forms etc. So it's better to route not rewrite. Also the MVC way gives you a single entry point for all requests to go through which can make it easier to manage things like Constants, and Autoloaders....
Oh well, this is broken of course:
(0-9a-zA-Z]+)
The charclass lacks the opening [ and doesn't contain/match a literal - as well.
Right. To get this working I needed to add QSA as in [QSA,NC,L]. After how many weeks!!??

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

How to redirect to a URL with a query string?

I've seen questions regarding redirecting a URL with a query string to a new URL that doesn't contain a query string.
Unfortunately I have to do this in reverse, my knowledge of redirecting in Apache isn't good, and I can't find any information about doing this.
So for example I need to redirect something like:
/news/news-item
to:
/news?item=news-item
The new URL structure is obviously not ideal, but this is something that is out of my control.
I've tried:
RedirectMatch 301 /news/news-item http://www.example.com/news?item=news-item
But obviously this doesn't work.
From what I understand I need to use RewriteRule, can somebody point me in the right direction?
I'm probably to late with my answer, but this is how I would have done it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) /news?item=$1 [R=301,L]
</IfModule>
I would also use RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d. In case a file or a directory with the specified name exists, it would not procede with the rewrite rule below.
I managed to figure this out after a bit of research.
I'm not sure if this is 'ideal' way of doing this, but it seems to work well from my perspective.
Instead of using a Redirect of a RedirectMatch it's possible to achieve this using RewriteRule, something like:
RewriteRule ^news/?(.*) http://example.com/?posts=list [R=301,L]
This will redirect any request to news or news/x (where x could be anything) to:
http://example.com/?posts=list
I should have made it clearer in my initial question that the query string didn't need to be generated using part of the old URL, if you need to do this I think you'll need to look into something like this:
How to redirect URLs based on query string?

htaccess redirect not working - tweak needed

i have a little issue that some of you may be able to sort please
in my htaccess i have
RewriteRule apply /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
basically meaning any get requests to apply will be sent on
however i have a page apply.html which with the above is no longer accessible as it creates a redirect loop
anyone know how to change the htaccess declaration so as only apply (and not apply.html) forwards on ?
tnx
Try to add a rewrite condition as well as limiting the matching pattern in the regex:
The condition will make sure that the rule will only be applied if the request isn't for an existing file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]

.htaccess 301 redirect that excludes a subpage

My rule looks like this:
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/?%{QUERY_STRING} [R=301,L]
My issue is that there is a page /page-parent/thanks that I don't want to be redirect.
I am also passing the query string along so that any ?gclid= string will go with.
I can't for the life of me figure out how to exclude a single sub page or all sub pages, which would work too.
Any help is appreciated.
You can check with a RewriteCond if the requested page is the page you want to exclude.
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L,QSA]
Make sure that you clean the browser cache when retrying since 301-redirects get cached.
Note: You don't need to append the query string manually.
You can use mod_rewrite to create a condition:
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L]
Note that you can leave out the ?%{QUERY_STRING} from your target, because query strings are appended to the target by default unless you've added new params via a ?.

Resources