URL Shortner... Mod Rewrite [closed] - php

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.

Related

PHP htaccess URL rewriting [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 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.

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.

htaccess 301 redirect domain.com/dir/trackback/ to domain.com/dir/ [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
As the title states, I've run into a few incoming traffic sources that are adding /trackback/ on the end of some of my URLs. The pages load fine due to my existing htaccess which grabs the dir name... But I don't want to generate tons of duplicate content with and without the /trackback/ addition. These incoming hits need to drop the /trackback/ off the end. What is the best way you would accomplish this without changing incoming links/sources?
I'm currently using php to check $_SERVER['REQUEST_URI'] and redirect back to the same URL without /trackback/ but I'm sure htaccess would be much faster because it wouldn't need to process the php engine of my site twice...
The easiest solution is a 301 redirect, which will flag the URL in most crawlers as if the URL they found (with /trackback/) permanently redirects to the version without. This would allow you to not have Google flag your content as duplicate.
You can do this using rewrite:
RewriteRule ^(.*)/trackback/$ $1 [R=301,L]
This will flag anything with /trackback/ at the end as a redirect to the page without.
NOTE: those hits are most likely bots looking for blogs with automated trackback acknowledgement in order to publish more links.

PHP: Two basic questions about URL [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
Question 1
www.example.com/?do=blah
How do you get it to work without index.php? Any examples?
Question 2
www.example.com/news.php?blah
This link doesn't show the same result as news.php, but shouldn't we be using news.php&blah=value for example? What does the string after ? stand for? Shortened version of GET variables or an entirely different thing?
Question 1
As long as index.php is your default and you have it present in your root folder then you only need to have www.domain.com/?querystring=value
Question 2
? is the beginning of a querystring parameter. It should be used for the first one.
& is for every querystring parameter after that.
index.php?querystring1=value&querystring2=value&querystring3=value and so on.
You can configure a URL rewrite using .htaccess for both of 'em.
RewriteEngine On
#for question 1
RewriteRule ^(.*)$ index.php/$1 [L]
#another example for question 2
RewriteRule ^news.php\?(.*)$ news.php?blah=$1 [L]
The first example will capture everything and redirect the user to the index.php file. So if the user tries to access domain.tld/abc they'll actually be accessing domain.tld/index.php/abc
As for the second example, it will grab the everything that is part of the query string (a better Regex might be needed). Basically, it'll turn news.php?value to news.php?blah=value
I don't entirely understand your second question, but the answer to your first question is that you need to specify a different default directory index on your webserver. Assuming you are running Apache, find this line in the httpd.conf file:
DirectoryIndex index.html index.php ...etc
and change it to:
DirectoryIndex your_preferred_default_page.php
For your second question, the question mark is not shorthand, it marks the beginning of a query string... it's used as a cue to treat futher strings as GET keys and values.

How to make form shows only value in the URL? [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 would like to build an online Malayalam to English and English to Malayalam dictionary.
There are two online dictionaries available, however it is not perfect so I have plan to build a good dictionary.
When I check some of the website both of them are used get method at the same time URL details are totally different method. Let me show how it working:
At the same time my website showing like this:
Is there any option to my URL like other two websites? I think both websites are using PHP with jQuery.
try this
RewriteEngine On
RewriteBase /
# make pretty urls work
RewriteRule ^dictionary/([^/]*)$ index.php?ml=$1 [L]
# redirect none-pretty urls
RewriteCond %{QUERY_STRING} ml=(.*)
RewriteRule ^$ /dictionary/%1 [L,R=302]
You'll also need some Javascript to catch the form's onsubmit, and change the url to not have the get parameters. You could do without, but that would result in an extra 302 request, and slow the pageload down a bit.
(PS make sure you php script return a 404 page if it can't find the word in the database.)
You need to use the mod_rewrite from apache2, it's a way for mask your parameters in a user friendly url.
You can read about thins in this tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
full docs: http://httpd.apache.org/docs/2.2/rewrite/

Categories