This is my first question:
I have created a URL Shortner Service in PHP.It works completely without any problem but there was a problem:
Someone who wants to access his url should type : MyDomain.com/go.php?u=key
Here i tried to remove .php extention by configuring apache and worked.Now it is like that:
MyDomain.com/go?u=key
but some services such TinyUrl.com works like that: TinyURL.com/key !!!!
How can i get this in php?
thanks a lot.
You basicly use mod_rewrite.
With mod_rewrite you can say that all requests which are like
www.example.com/[A-Za-z1-9]
are redirected to:
www.example.com/shorturl.php?key=$1
While $1 is the extracted variable from the requested URL.
There is no proper way to do it with pure PHP.
The rewrite rule could look like this:
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
I would exclude files which really exists from the rewriting, use for this
RewriteCond.
This could be done like here:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]
(Source: anubhava at RewriteCond to skip rule if file or directory exists)
create .htaccess file to redirect any request to your domain to one file lets say go.php
so in .htacess do like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ go.php?key=$1 [PT,L]
</IfModule>
This redirection any request like mydomain.com/userkey to mydomain.com/go.php?key=userkey
now in your index.php you can do your redirection login.
<?php
$key = $_GET['key'];
// your logic here.
?>
This ref will solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ redirect.php?c=$1 [L]
</IfModule>
Related
I am trying to use .htaccess file for my REST service but I found that the url are not actually redirected. The web server show
I wanted to use url such as
/api/v1/test
instead of
/api/v1/rest.php?request=test
I have already check mod_rewrite is acutally working.
My .htaccess file is as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/v1/(.*)$ api/v1/rest.php?request=$1 [QSA,NC,L]
I have already look at PHP redirecting problem and Sitemap redirecting
you can try this rule instead because it works with me :
RewriteRule api/v1/([A-Za-z0-9-]+)/?$ api/v1/rest.php?request=$1
try putting .htaccess file in htdocs directory and if the directory api location is
C:\xampp\htdcos\xampp\www\api
then put the full path in the rule
this site generates mod_rewrite for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/v1/([^/]*)$ /api/v1/rest.php?request=$1 [L]
I have the following code on my htaccess:
RewriteEngine On
RewriteRule ^course-details/([a-zA-Z0-9]+)/$ course-details.php?id=$1
I think this gave me a URL like this: /cp/course-details/5 where 5 is the id of the course.
But when I go to this address I get 404 not found. Also, our current url is
/cp/course-details.php?course-details.php?name=bla-bla-bla&category_id=1
and we need a friendly url like this
cp/category-name/course-name/
Thanks in advance for you help.
You can try this in your htaccess file if the php file is in the cp dir. Since it appears your are developing in a subfolder(peppers) of your dev box you can try this.
RewriteEngine On
RewriteBase /peppers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
If this is for production then you can use this in the .htaccess file in the root.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ /cp/course-details.php?name=$1 [NC,L]
RewriteRule ^course-details/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/.*$ course-details.php?id=$1&category_id=$2
With that, you can use URL like course-details/1/5/category-name/course-name
Because URL allow: course-details/category_id/id/what-ever-you-want
I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!
I have a URL that is being generated using GET variables from a form in PHP. The issue that I have is that there are two variables being passed. Here is an example URL to clarify:
http://www.examplesite.com/example.php?first=one&second=two
I would like to use a mod_rewrite in my .htaccess to make this a smaller URL. Ideally I would like the URL...
http://www.examplesite.com/one
to be able to redirect to the full URL...
http://www.examplesite.com/example.php?first=one&second=two
but as you can see there are two variables. Is this possible? If not, what is the shortest that I can get the URL using both variables?
Here is my current attempt to solve this with a mod_rewrite
RewriteEngine on
# don't rewrite if the file exists
RewriteCond %{REQUEST_FILENAME} !-f
# don't rewrite if the directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ example.php?first=$1&second=$2
Try this:
RewriteEngine on
# don't rewrite if the file exists
RewriteCond %{REQUEST_FILENAME} !-f
# don't rewrite if the directory exists
RewriteCond %{REQUEST_FILENAME} !-d
# http://www.examplesite.com/one/two
RewriteRule ^([^/]*)/([^/]*)$ /example.php?first=$1&second=$2 [L]