I have a problem and I don't know if it's possible but I'm going to ask here.
I have rewrite rules like this:
RewriteRule ^login$ index.php?page=login [NC]
and I want to do something like this, if you add "/success" then the url will look something like this:
"mydomain.com/login/success"
and that will add a $_GET variable to the url and the original url will be:
"index.php?page=login&status=success".
I want to be able to add /success to every page that I have, example: mydomain.com/register/success and so on..
Thanks!
Allright, first thing:
This is not how URI Rewrite works. Sorry man but you really need to study a bit more on the topic.
To solve your issue you need to:
Get the URI using PHP Global Vars
Process it and extract your parameters
There is no way .htaccess will be able to do what you are looking for. I don't usually post my stuff, but i suggest you to read some of the source code of a project i've been working on a few months to understand how routing works. URLParser Example here
Unfortunately to properly understand my code you have a minimal notion of MVC. Hope it helps!
Related
Hey so im working on a website and one part of it allows you to lookup a user based on their name. At the moment i have it using a $_GET request so the link would look like:
http://website.com/p?name=John+Smith
How would i be able to remove that ?name= because i see alot of sites doing things like:
http://website.com/p/John+Smith
how would i achieve this because to my knowladge their arent any other forum request types only Post and Get?
URL rewriting is definitely what you're looking to do. It's well worth playing carefully with it but lots of testing is recommended. With great power comes great responsibility!
Most dynamic sites include variables in their URLs that tell the site what information to show the user. The example you provided is exactly like this.
Unfortunately, a cleaned up URL cannot be easily understood by a server without some work. When a request is made for the clean URL, the server needs to work out how to process it so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, you need to first create a text document called ".htaccess" to contain the rules. This would be placed in the root directory of the server. To tell the server to rewrite a URL pattern, you need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^p/[A-Za-z\+]$ /p/?name=$1 [NC,L] # Rewriting rule here
The NC bit denotes case insensitive URLs and the L indicates this is the last rule that should be applied before attempting to access the final URL.
You can do quite a bit with this one rule, but the specifics extend far beyond the space of my answer here.
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I would highly suggest reading that thorough guide to help you on your quest!
Before I go ahead and ask you my question, I understand that there are many methods on the internet to accomplish this sort of thing. I understand also that this process is called rewriting urls. However, since I am not a proffesional at coding, the process of these things seem very daunting and hard to understand. So, I thought I'd ask the StackOverflow community for help!
Here's what I'd like. Currently, I have 2 main files.
index.php and php.php.
My site allows users to enter a username and get information for that username. index.php holds the form input, and php.php handles the actual PHP Code. Once a user has submitted the username, the URL at the end will look like this;
http://mcspy.info/php.php?username=_scrunch
What I'd like the URL above to look like is:
http://mcspy.info/player/_scrunch
Could somebody here please guide me and explain in an easy manner to understand, how I would go about doing this? Preferably using PHP! Thanks a lot!
Well it would be much easier to do this with a .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^player/(.*)$ php.php?username=$1 [L,NC]
The easiest and cleanest way is with .htaccess files, but if you don't want to, you can use PHP, what I'm not sure but this would help URL rewriting with PHP
I sincerely apologize if this is a duplicate. I've looked at so many threads but none seem to apply specifically to what I'm trying to do.. and for some reason, I just have such a difficult time wrapping my brain around .htaccess rewrite rules!
I purchased a php script and after installing it, I create a new post and it works fine.. except of course, it shows: mydomain.com/post.php?id=7
Want I want is to show the title of the post after the .com/
I know some will advise to use a post id or a date or something, but I'd really prefer it to be written as stated. So the end result would be: mydomain.com/this-is-my-first-post
I then want to be able to correctly call this page. I assume this would all be done in the index.php page (which calls the post.php page)?
I found the line in index.php that currently calls the post and it looks like this:
<h1><?php echo $theArticle['title']; ?></h1>
Is that also what I would need to update?
Thanks for any assistance. I really have tried but I am just not programming minded and struggling mightily!
Your question is a bit too broad, but if you are willing to compromise, you could change the url to something like:
mydomain.com/post/7/the-title-of-post-7
And then you would only need a rewrite rule and no changes in the database retrieving code.
In that case the rewrite rule would look something like:
RewriteRule ^post/(\d+).*$ /post.php?id=$1
^^^^^ capture the number to be used
You would of course also need to rewrite all links to the required format.
i have a large database and i would like to url rewrite. it is like
www.example.com/products.php?id=111&cat=99&f=6&hjk=4545..
So I wrote url rewrite for this code to appear as
www.example.com/men/watches/casio/latest.. like that and its works fine
The problem is that I have a large database and its nearly impossible to write all url rewrites separately.
I thought of generating .htaacess file automatically using php. Does that make sense? or whats the best solution for this..?
EDIT: For #ghoti
RewriteEngine On
RewriteRule ^/([^/]*)/check/([^/]*)/SubjectId/([^/]*)/UniversityId/([^/]*)/CourseId/([^/]*)\.html$ /dsh_course_subjects_topics.php?TopicId=$1&check=$2&SubjectId=$3&UniversityId=$4&CourseId=$5 [L]
You need a single rewrite rule to catch the pattern you are looking for and pass it to a php page. For example:
(note, all of this needs to be on the same line)
RewriteRule
^/(men|women)/([a-z]+)/([a-z-])/(latest|newest|expensive|cheapest)$
/products.php?gender=$1&product=$2&brand=$3&filter=$4
In that way, you can pass generic request on to PHP where you look at $_GET to find out what the dynamic values were, and then pull up the correct items.
If you look at StackOverflow, their URLs look like this:
^/questions/([0-9]+)/([a-zA-Z0-9-]+)$
Spend some time looking at ModRewrite documentation as well as Regular Expressions. Then think how you can positively identify your product (eg, you may need to incorporate an ID in the URL nonetheless).
I have been looking and looking around on the web for an answer for my question. But everything is just not the right thing.
So my issue is:
I'm creating my own CMS, and right now I've got the issue with the urls. They aren't really that SEO friendly.
When I create a new page, it gets the URL: index.php?page=(id). That doesn't tell much. So what I would love to create.
Is that I wan't the URL to be something like: www.myurl.com/home instead of the page=id. Is this possible?
I have to mention, that I need the id number later on, for editing the pages. I'm focusing the GET function to be able to edit my pages, and to show 'em one by one.
Thanks. :o)
Try to set your .htaccess file to the following:
RewriteEngine On
RewriteRule ^([^/]*).html$ index.php?page=$1 [L,NS]
this way you can translate what visitors see as yourdomain.com/home.html to what php reads as yourdomain.com/index.php?page=home afterwards you can of course use a translating array containing your id's
$translationArray("home"=>1, "contact"=>2);
$id = $translationArray[ $_GET['page'] ]; // $id now contains 1
What you're looking for is called Semantic URLs. Other keywords that will aid you: .htaccess, mod_rewrite
A full solution is too complicated to expand upon here but the underlying idea is fairly simple.