URL Shortening in Apache 2 - php

I have a site with homepage as /var/www/mysite/index.html
Now I want to direct www.mysite.com/x2312d to /var/www/mysite/app/pass.php?id=x2312d
How do I do this?
Where should I create .htaccess file and what should be the contents of it?

in your root /var/www
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ /app/pass.php?id=$1 [NC,L]

put the .htaccess file in /var/www/mysite/app/. I'm assuming your docroot is /var/www/mysite/
make sure apache 2 has the rewrite module enabled
.htaccess:
RewriteEngine On
#allows you to still access static items in this dir
RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g)$
#sent it all to pass script
RewriteRule (.*)$ pass.php [L]
I like this method, assuming pass is some sort of controller. My controller for a rest api parses the /x23123d manually from the $_SERVER global var, but you could use a different rewriterule to have it be in the $_GET/$_REQUEST['id'] global var.
something like this:
RewriteRule ^app/(.+)$ app/pass.php?id=$1 [PT,L,QSA]
Good References: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
edit: almost forgot, don't forget to handle trailing slashes too.

Related

Allow specific file in htaccess that is rewriting all subdirectories

Not sure if I titled my question accurately. I have a directory structure like this:
example.com/edit/43424242/
Where the number at the end is not actually a folder. It's really just a variable that I want to use at the /edit/index.php directory.
I tried using this in my .htaccess file at the root directory:
RewriteEngine ON
RewriteRule %{QUERY_STRING} ^$
RewriteRule ^edit/(.*)/?$ edit/index.php?var=$1 [NC,L]
But that didn't work because it messes up my AJAX post to another file in that directory /edit/post.php
So I thought maybe I need to modify my .htaccess file so that I can still retrieve the subdirectories as a variable and still be able to use post.php or perhaps modify .htaccess so that it allows index.php to communicate with post.php.
Any ideas?
RewriteRule %{QUERY_STRING} ^$
It is not enough for check for empty query string as it will not prevent this rule affecting serving real files and directories.
You may use this rule:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^edit/([\w-]+)/?$ edit/index.php?var=$1 [NC,L,QSA]

Remove php file extension from the url

I know this question has been asked like a 1000 times and I have probably tried like a 1000 suggestions as well, still no go.
I would like to remove the php file extension and substitute it with a slash (the slash is not so important but to remove the extension is).
My project is located here:
localhost/~fn/MyProject/
It contains two folders, public and includes. So all the public files are in the public folder: localhost/~fn/MyProject/public/index.php
I have tried so many suggestions already but most of them simply don't work. I am getting either a Internal Server Error, Forbidden or 404. I am putting the .htaccess to the public folder. Mod rewrite is on. No success with anything on stackoverflow and neither external resources ( e.g. http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ ). For example using the rewrite rules from the metioned webpage shows me 403 Forbidden to even access the index.
Any hints of what I may be doing wrong? I am really stuck. Thanks a lot!
If your htaccess is in public project folder, try with this code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*public/(.+)\.php
RewriteRule ^(.*)$ /~fn/MyProject/public/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /~fn/MyProject/public/$1.php [L]
If the directory has an Index.php in it, the url will not show the file name when you browse to that folder. example.com/index.php would just show as example.com. You could use PHP includes to pull in each page to the index page in order to hide the file names but this isn't the best practice in general.
Put this in a .htaccess file in localhost/~fn/MyProject/ (so the file will be localhost/~fn/MyProject/.htaccess):
RewriteRule ^(.+?)\.php$ $1/ [QSA,L]
This captures anything ending in .php, strips off the .php, adds a /, and appends any query string as needed.
If you want only remove extension ".php" from index.php you should use mode_rewrite
RewriteRule ^index.php$ index // or
RewriteRule ^index.php$ /
But best way is implements mechanism to rewrite all urls and manage in your front script (index.php);
If you want use mode_Rewrite you shuold check if your mod_Rewrite is enabled on your Apache server. Go to apache.conf and check if line
LoadModule rewrite_module modules/mod_rewrite.so
is uncomented, dont forgot restart apache!
than you can On rewrite rule type in your apache.conf
<IfModule mod_rewrite>
RewriteEngine On
</IfModule>
or include something like in .htacces
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?%{QUERY_STRING} [L]
Now all signs from urls will going to your once index.php. Here you can do with this everything what do you need to do for example u can call some controller.
If you get 403 Forbiden dont forgot check chmod of the file.
To remove the .php extension from a PHP file for example yoursite.com/demo.php to yoursite.com/demo, you can add the following code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

PHP Short URLs like on Wordpress

Does anyone know how to have a kind of URL Shortner?
for example, using PHP rather than having www.domain.com/page.php?seq=1435 to have www.domain.com/rGhpf
Does anyone have any ideas on what i can do with this - any help would be much appreciated
You can do this using Apache's Rewrite urls.
RewriteEngine On
RewriteRule ^seq/([0-9/]+)$ /page.php?seq=$1 [L]
That means you can goto http://domain.com/seq/1435 instead of http://www.domain.com/page.php?seq=1435
Ensure Apache's mod_rewite is available http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Create a file called .htaccess in the docroot of the site (you can also do this in other directories with other rules)
If script is index.php all okay. No need to continue L=last
If script doesn't exist or directory doesn't exist redirect to index.php (we know its okay) and put requested script in $_GET['seq']
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?seq=$1 [L]

How to make a PHP GET Request method like this "http://somesite.com/games/213123"?

Hi guys I'm wondering how to make this kind of requests to the server I looked at many sites and they use this technique. For example gametrailers.com => http://www.gametrailers.com/video/level-six-diablo-iii/**721239**. I know that a GET request is made by using parameters like this: http://somesite.com/page.php?param=1. So how to make it like gametrailers. I hope you understand my question and I doubt that "721239" is a folder on the server with an index page inside of it.
You need to create a file placed in the folder near your script with name .htaccess
In this file you need to define rewriting rules. The contents of the file are:
RewriteEngine on
RewriteRule ^games/(.*)$ games.php?id=$1 [L]
In this case
http://somesite.com/games/213123
will be transformed into http://somesite.com/games.php?id=213123
The more convinient way is to do url rewriting. (wiki)
For example, you can have a .htaccess like this, well explained in this guide from symfony:
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
You can achieve this using MultiViews in Apache2 (http://httpd.apache.org/docs/2.0/content-negotiation.html).
MultiViews (when enabled) instructs Apache to build a parameter map when a resource doesn't exist, so for www.foo.com/app/game/14, if www.foo.com/app.php exists and app/game and app/game 14 don't, it can be set up to translate that to www.foo.com/app.php?type=game&article=14
Some people also use mod_rewrite, but I'm not sure that's the preferred approach.

url rewriting index.php

i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here

Categories