PHP htaccess URL rewriting [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am currently writing an app where you can add a comment to a post based on IdTicket.
<td>Comments</td>
I am passing the Id value in my URL which is a part of the table. It is a dynamic value, different for every ticket. I am using $_GET['IdTicket'] on the comment.php page to get the id and based on that write a comment.
Is there a way to rewrite that link in htaccess? It would be perfect to hide everything or just make it prettier. And how would it look applied to that table?

Sure that is possible. And there are already countless examples for this...
You need to change the reference you hand out in your spplication logic:
Comments
And you need to take care to internally rewrite the incoming request to the actual resource again:
RewriteEngine on
RewriteRule ^/?comments/(\d+)$ /comments.php?IdTicket=$1 [QSA,END]
Obviously the rewriting module needs to be loaded. And if you want to use distributed configuration files (instead of the real host configuration inside the http server) you also need to have enabled the interpretation of those for host and location.

Related

Simplest Routing For URL Queries [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm stuck trying to figure out the best approach to my problem. I want to pass query params with a / than a ?.
So for example, instead of:
https://example.com/search.php?q=test
It'd be:
https://example.com/search/test/
I'm not using any PHP framework. How can I reach my goal?
You could do this with your .htaccess file.
For that you need to write in your .htaccess:
RewriteEngine on
RewriteRule ^search/([A-Za-z0-9\-]+)/?$ search.php?q=$1
The first line activate your RewriteEngine, so you can write RewriteRules as much you like.
In the second line you define your first RewriteRule:
In the URL it need to be search/([A-Za-z0-9\-]+)/.
That means, you only allow the characters inside the square bracket.
Then you redirect to your wanted script, in this case its search.php with the GET Parameter.
It's also possible to do this with more parameters or much more. For that you should google maybe "htaccess Explanation".
Better try php router and dont waste the time for rewrite !!!
And test nginx server it is awesome.

A specific htaccess inquiry [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
After a bit of research I think its better if I ask for help to a specific problem, because I think if I am to learn the skills needed, I'd be taking on another language, and I can't get into the details of htaccess and regular expression at the moment, hopefully someday.
What I have now in URL is (localhost/site/controller/function/$arg1)
What information do I need to place into the htaccess so the controller and function isn't displayed. But the end result is (localhost/site/$arg1).
I think, this doesn't really make sense. As stated in the comments, you can use mod_rewrite to send your shortened URLs to the controller, but controller and function need to be static. So having a separate function doesn't really help, controller should be enough.
One thing you could do is selecting the RewriteRules in your .htaccess depending on the $arg1 using for example RewriteCond. That way you could send different URLs to different controllers. It depends on your URL structure if this is feasible.

Shortened url redirect? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need help to get this done.
My webpages have formatted urls in this pattern: "http://qifu.us/index.php?page=item&id=4" for example - if there are more pages, only the last page id number will be different.
I want to get this part "index.php?page=item&id=" out, and shortened like "http://qifu.us/s4", when a user input the shortened url into to address bar, it will be directed to the right page, which is the true url.
I am thinking to save the STATIC part "index.php?page=item&id=" into a string variable, and append the DYNAMIC page id - which is 4 in this example, then use Javascript or PHP to direct to the right page. But I don't know how the steps, pls help. Thanks.
Actually an htaccess will be very good for this purpose.
For urls like http://qifu.us/s4 do the following: Create a file with name .htaccess and place at your root directory with the following content.
RewriteEngine On
RewriteRule ^s([^/]*)$ /index.php?page=item&id=$1 [L]

php profiling with names in the url "mod_rewrite" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
am creating a website for a company in php i need to create to every member his profile page
like facebook's url
www.mywebsite.com/my.name
not like this:
www.mywebsite.com/?profile_id=65497
any ideas ??
I asume you are aware that this can only be done if you have some source that maps ids to names. mod_rewrite can look up strings in a lookup table and use the result in a substitution.
For example suppose you have a file that contains the name to id mappings.
# Mapping of names to UserIDs
Bill.Gates 100
Barack.Obamaq 101
Arsan.Gamal 102
You could then add the following to your apache config:
RewriteEngine On
RewriteMap UserIDs txt:/path/to/userids.txt
RewriteRule /(.*) /?profile_id=${UserIDs:$1|NOTFOUND} [PT]
Now when a request comes for /Bill.Gates the part after the / is captured in a regex back reference, and used to look up a value in userids.txt. This value is than used in you substitution.
There are other possibilities. If your usernames and IDs exist in a database you can use a database query in a RewriteMap. More info on what you can do with RewriteMaps, and how to do it can be found in the manual here:
http://httpd.apache.org/docs/2.4/rewrite/rewritemap.html

URL Shortner... Mod Rewrite [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to build a simple URL shortner and I have come across a few complications:
A) in .htaccess how would I rewrite mydomain.com to make it so users can enter mydomain.com/4854 while the page is actually mydomain.com/url.php?key=4854.
B) If I do have that Rewrite rule how can I be sure to bypass it for index.php and add.php
Thanks so much in advanced.
In your .htaccess you can do:
RewriteEngine On
RewriteRule ^([0-9]+)(/)?$ /url.php?key=$1 [QSA,L]
This rewrites requests that contain only numbers to url.php.
Then in url.php, if key is not found, redirect to error page.
RewriteEngine on
RewriteRule ^([0-9]+)(/)?$ url.php?key=$1
And if the key is invalid, redirect in the php file
Let me save you the grief...I was doing the same thing, and found it written for me at yourls.org. It covers pretty much everything you need, as well as giving you option user pages to add their own, AND gives you an API interface for others to use the shortened URLS. I had it up and running in minutes, and spent a couple of hours modifying the samples given to be ready for production.
Even if you don't use it, the mod_rewrite solution is there for you.

Categories