Wrong redirection with .htaccess? - php

I made an URL Shortener and shortened links looks like this:
http://patison.eu/06uoM. My PHP code checks if this 06uoM is in my database - if yes it will redirect to the correct page, if not it will open index.php file from index_html directory. It's bad because when I write URL to image uploaded on server it will open index.php because URL is not in database...
http://patison.eu/forum/images/off.gif - see?
This is how .htaccess looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1
Please help me solve this problem!

It sounds like you want to let 404 errors happen on URLs like
http://patison.eu/forum/images/off.gif
but not on URLs like
http://patison.eu/06uoM
If that is the case, you can change your .htaccess code to this to limit the type of URLs rewritten:
RewriteEngine on
# Limit rewrites to urls like '/06uoM' or '/06uoM/', but not like '/forum/images/off.gif'
RewriteCond %{REQUEST_URI} ^/[0-9a-z]{5}/?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1

Related

Simple htaccess rule for rewriting the URL

Im trying to achieve this
http://domain.com?q=page-slug to http://domain.com/page-slug
right now if I access this http://domain.com?q=page-slug my webpage is working, but I want clean URls like http://domain.com/page-slug
I used this code in my htaccess but returning 500 error
RewriteEngine On
RewriteRule ^(.*)$ ?q=$1 [L,QSA]
any help please.
Try this I am assuming you have index.php page which is handling the request.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?q=$1 [QSA,L]
If not index.php change to actual page you have.

php friendy url using htaccess

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

Rewrite URL for Profile Page

How do I use .htaccess to rewrite:
mysite.com/profile/[username]
go to:
mysite.com/profile.php?id=[username]
I don't want to have to reload the page just change the URL to make it look good.
Any help is appreciated.
This is the htaccess rule that should get you started:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule profile/(.+) profile.php?id=$1

Creating URL Shortner service

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>

hide extension .php in url mod_rewrite

I have a website in PHP, and I'm trying to hide the extension. I've found a couple of things via Google, but they all seem to be too complicated, or they redirect index to index.php: like if you write name.com/contact it goes to name.com/contact.php. I don't want that, I just want:
www.name.com/es/index.php to be www.name.com/es/inicio
www.name.com/es/empresa.php to be www.name.com/es/empresa
www.name.com/es/productos.php to be www.name.com/es/productos
www.name.com/es/clientes.php to be www.name.com/es/clientes
www.name.com/es/recetas.php to be www.name.com/es/recetas
www.name.com/es/contacto.php to be www.name.com/es/contacto
www.name.com/en/index.php to be www.name.com/en/home
www.name.com/en/company.php to be www.name.com/en/company
www.name.com/en/products.php to be www.name.com/en/products
www.name.com/en/clients.php to be www.name.com/en/clients
www.name.com/en/recepis.php to be www.name.com/en/recepis
www.name.com/en/contact.php to be www.name.com/en/contact
the /en/ folder is English version and the /es/ folder is the Spanish version
Also the links inside the website, would be also without the extension.
E.g, an image linked to clientes would be clientes and not clientes.php and if somebody tries to go to /page.php, it still works but it redirects to /page.
The htacces file should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You redirect any php file in the url with the same name withouth the "php".
Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.
So, at the beginning of each php file you should call this function :
function redirectIfNeeded(){
$url = $_SERVER["REQUEST_URI"];
if(preg_match("/\.php/$", $url))
header("Location: ".preg_replace("/\.php/",$url));
}
Please crate a .htaccess file in root directory an use this code!!
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Hope!! it would be useful for someone!
RewriteEngine on
Rewritecond %{REQUEST_URI} !-f Rewritecond %{REQUEST_URI} !-d Rewritecond %{REQUEST_URI} !-l RewriteRule ^([\w\d-]+)$ $1.php [L]
RewriteRule ^([^/.]+)$ $1.php [L]

Categories