htaccess rewrite URL with multiple php parameters - php

So this is likely a duplicate, but I have exhausted every example on here and nothing works.
I am working on an adult site for someone, and using it as an excuse to learn something along the way.
Take this URL:
http://www.websitename.co.uk/profiles/masseur/profile.php?id=5&name=Erika%20S
I want it to look like this:
http://www.websitename.co.uk/profiles/masseur/Erika_S
So 2 things need to happen:
URL rewrite without being redirected, disregarding the id parameter
spaces in the name being replaced with an underscore (or a dash)
Rewrite is on. Other rewrites work such as removing file extensions (is this interfering? Although it don't work even if I disable this.)
I am losing whatever hair I have left.
please help!

How do you know which id to use? You could possibly use a Rewritemap to look it up, but it depends on how you know what it is (As Heinrich pointed out in the comments).
Also translating the '_' to a space, though possible, would be much easier to do in profile.php that in the rewrite. What's slightly confusing is you say you want to translate spaces to underscores, but in your example you do the opposite (to encoded spaces at least). Could you clarify, or correct my misunderstanding.
Here is a base rule to start you off
RewriteRule ^/profiles/masseur/(\w+) /profiles/masseur/profile.php?id=5&name=$1

Related

RegEx for URL pattern(s), redirect after re-launch

I have been struggling and testing for the last two hours and simply cannot wrap my head around the whole RegEx-stuff enough in order to find a proper solution to this...
I am trying to redirect a couple of URLs from our old site to the new one due to a recent re-launch.
This is the current state of things / a demo of my RegEx
Essentially it looks like this:
.+(\/es|\/de|\/en)?\/(legal)(.+)?
My problem is that a URL like https://example.com/es/projects/legal-yeah is also being matched, which does make sense looking at the rule but is not what I want to achieve...
How can I perform a test which only matches URLs where there is nothing in between the first part for the language string (de/en/es/empty) and the second part (/legal)?
Thanks so much for sharing your thoughts on this, appreciate it!
By using an end-of-line anchor $ and explicitly adding (\/.*) after legal you can achieve what you need:
.+(\/es|\/de|\/en)?\/(legal)(\/.+)?$
https://regex101.com/r/HsIDkQ/8
This final RegEx-rule matches the URLs like I intended – ignoring any other occurences of the "legal"-string (in this case) which might appear in another URL on some other level and 'fuzzy' enough to include all the language-cases, even without a language-string appearing at all.
Solution
The trick in the end was to force the rule to look for a TLD in front of the other stuff so it would only allow for first-level URLs to be included.
UPDATE: My first solution didn't turn out to work since the redirection engine / plugin only makes use of the URL path, not including the domain (see GitHub issue) and as such I can't match the DOT as needed precessor.
Now the rule is paying attention to the start of the string and not accepting anything other the language string in front of the targeted URL-slug which in turn removes false positives.
Thanks to #Xatenev who pointed me in the right direction!

GET request without needing the ? at the end

I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?
For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones
Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.
But how can I change that link above to:
http://www.example.com/user/JimmyJones
Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?
EDIT:
Here are some websites that do it just about how I would like to do it:
imgur.com/user/example
facebook.com/exampleuser
you make .htaccess file in the root dictionary then start it with
RewriteEngine on
then write your rules, For your example it would be like this
RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]
so a full page would be like this
RewriteEngine on
RewriteRule ^/?user/([^/]+)/?$ user.php?user=$1
just for your example.
In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:
RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]
The first line says use the rewrite engine.
The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).
The L flag basically says job done.
If the .htaccess were in the public root:
//example.com/user/JimmieJones
would map to:
//example.com/user.php?user=JimmieJones
However it will not match:
//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9
Note that any existing links in your application:
Visit Jimmie's Profile
Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.
Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.
I reserve first level patterns for aliases/shortcuts/campaigns.
//example.com/slug
I'd avoid that for your user profile urls if possible.
You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).

Need some answer about example.php/example

I am doing on a project and i stumble upon on this while going through some code. I had tried to google but could not find a correct keyword to search regarding this question.
http://www.fruit.com/example.php/red
I am confuse with this and the end of a .php it still have a character /red for example.
What is the purpose of this? Does it have something to do with POST method?
A possible explanation is that the developer is using .htaccess rewrite rules. It is most likely extra data that has to do with the page (such as you mentioned, POST and GET) and it's only done this way to make the URL look prettier or to simplify the URL.
It is also sometimes done this way to make it easier to linkback to this page, especially if the page is being generated based on the data.
A popular place to do this would be blogs. You often see blogs display links as:
http://blog.com/blog/2001/02/23/how-pretty-is-this
when in reality, the request to the server is something like:
/blog.php?year=2001&month=02&day=23&title=how-pretty-is-this
which isn't that pretty and not as easy to link-back to this particular page.
Mr.Xenotype answered your question well but missed one little part.
Does it have something to do with POST method?
No, it deals with the GET method. As he stated, it's pretty much to make the URL look "pretty", and it could actually be a crucial part of a RESTful API (bit advanced for a beginner) if that's how the site was designed.
http://www.fruit.com/example.php/red
Is the same as
http://www.fruit.com/example.php?id=red
Some tinkering with the .htaccess file is required to accomplish this using what are called regular expressions, or REGEX. If you'd like to know more I'll gladly provide some examples.
Adding a "RewriteRule" to the .htaccess file (a server configuration file) can allow the server to change the way the server files/folders are accessed
//probably what the rewrite looks like of the example you provided
RewriteRule .* example.php/$0 [L]
RewriteRules are broken into 4 blocks: action, pattern, rewrite and flag
Action: RewriteRule, essentially just lets the server know we're going to be performing a rewrite
Pattern: .* is our pattern in this example. The "." stands for any character, meaning match any character, and the "*" tells it to repeat this. So, match any character after the rewrite.
The rewrite is the page to perform this on, so example.php is the rewrite. The "$" of this tells it where to start the rewrite. So, we'll start the rewrite at the "$" symbol, and use our pattern. Our pattern ".*" tells us to match any character, in this case, red.
The [L] means last rule. This is it, no more rules to follow. You can use multiple rewrite rules for multiple pages.
So now, instead of using example.php?id=red, the server now knows that starting after "example.php/" to match any characters we supply.

Need help writing URL Rewrite rule Regular Expression for PHP and htaccess

I've read a lot of sites talking about how to use Rewrite Rules and Regular Expressions, but they all tend to be for specific examples without breaking down every piece of the syntax and actually helping me to learn the concepts so that I can write the type of code I need.
Any help is much appreciated!
I have a url like this http://www.mysite.com/myphp.php?x=1A&y=2B&c=some1&d=some2
I would like this to rewrite like this: http://www.mysite.com/some1/some2
It seems really simple, but as I mentioned, I haven't run into a site explaining the concepts in a way that I could figure out how to make this work.
In this particular example, ?x= is always the same, while 1A varies. The same applies to each of the other pieces of the php string, however, I'm assuming the rewrite rule should handle any number of characters between the .php file and the variables I'm actually interested in.
Thanks again to anyone who can help explain this or provide a high quality resource on both rewrite rules and regular expressions to help me learn it.
A rule like this will work (tested):
RewriteRule ^([^/]+)/([^/]+) myphp.php?x=1A&y=2B&c=$1&d=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, we are saying - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored (backreferences)
As for high-quality resources on mod_rewrite, in the past I've found a few by searching for "mod_rewrite tutorial" (I thought this was a good one for a beginner). They'll mostly be like that, so if there's anything specific you don't understand, get that out of the way before moving forward. I'm not sure how else the syntax can be explained.
Regexes are a different thing, again, many resources out there - including for beginners. Once you get the basic hang of regexes, you can use the Python re page as a reference.
Something like (not tested) added to .htaccess?
RewriteEngine on
RewriteRule ^/([0-9a-z]+)/([0-9a-z]+)/$ /?c=$1&d=$2 [L]
assuming x and y are not of interest.

URL rewriting with 3 variables

I want to do URL rewriting of my webpage. There are 2 sorts of links possible on the same page as follows:
Pagination:
http://www.xxxxx.com/dictionnaire.php?page=4
That I want to look like this:
http://www.xxxxx.com/dictionnaire/p4
Word:
http://www.xxxxx.com/dictionnaire.php?idW=675&word=Resto-basket
That I want to look like this:
http://www.xxxxx.com/dictionnaire/675/Resto-basket
In the .htaccess, I have the following:
RewriteRule ^dictionnaire/p([0-9]+)?$ dictionaire.php?page=$1 [NC,L]
RewriteRule ^dictionnaire/([0-9]+)/([a-z])?$ dictionaire.php?idW=$1&word=$2 [NC,L]
QUESTIONS:
Is this the best google friendly way of doing this? (mostly for the word link, or is there better?)
Can you have 2 rewrite rules for one link? Like above?
Is there an error in my code, is so, please help.
When I created this code, my CSs and images weren't appearing. Can you help me fix it?
I know it's a long question, but I thought it would be easier that way.
Thank for the help.
Is this the best google friendly way of doing this? (mostly for the word link, or is there better?) Well, you could put page4 in the Pagination part, either won't really affect it much. For the Word page, why do you have it find words by IDs instead of the actual word? the word=(WORD) doesn't really seem to do anything at all. Perhaps remove ID entirely and have it search by word so that it could be dictionnaire/word/(WORD) instead.
Can you have 2 rewrite rules for one link? Like above? Yes, it is completely possible to have more than 2 rewrite rules (Think many forums that do this)
Is there an error in my code, is so, please help. I haven't looked hard, but it doesn't appear to be any errors.
When I created this code, my CSs and images weren't appearing. Can you help me fix it? The problem here is it is searching /dictionnaire/p4/css.file.css for your css file. It isn't looking in your root directory like I suppose you want. Put the direct root to your CSS file starting with a / at the beginning.
This should be "Google friendly".
Yes, rewrite rules are applied in order; as soon as one matches and replaces the URL, it's unlikely any further ones will match (since they'll be working on the replaced URL of the previous one).
Looks OK to me. You could make it into one rule though, if you allowed the destination URLs to be a little different (to both use page= rather than idW= on the second one).
That's because the browser will ask for resources relative to the non-rewritten URL (it of course doesn't know about what's going on behind the scenes). You'll have to use absolute URLs for your images and CSS (or alternatively, use ../ in the URLs, or add more rewrite rules for your resources to make them work).
Hope that helps.
EDIT: Sorry, because you have that [L] at the end of your rules, it will stop trying to match any more rules once it matches one. This won't have any practical effect in this case.

Categories