PHP page access without extension and variable - .htaccess

How can I create a PHP page system that we access without .php extension and without ?page= variable?
There's a simple exemple:
http://www.exemple.com/portfolio/1
Instead of:
http://www.exemple.com/portfolio.php?id=1
It would be great for me because I'm a web designer and this is a thing I never did. So it would be good to know this! Also I will use this in my website.

you would need the following in your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule .+ %1.php [QSA,L]

You're looking for mod_rewrite.
RewriteEngine On
RewriteRule ^/profile$ /account.php?profile [L]

Create .htaccess file in your web root and enter following.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^account/([0-9]+)$ account.php?id=$1
OR
See mod_rewrite, in case URL rewriting, if you trying to do such.

You can use multiviews, or better and more used: mod_rewrite module from apache. It can be via a .htaccess file where you create rewrite rules like
RewriteEngine On
RewriteRule ^(.*)?(.*)$ /$1.php?Action=$2 [L]

Related

Redirect old page to new page use htaccess

I have new Content Management System which rewrite old static code.
I want to redirect some old page to some new page, for example I want to redirect 3 pages only to 3 new pages:
domain.com/oldpage1 => domain.com/new/newpage1
domain.com/oldpage2 => domain.com/new/newpage2
domain.com/oldpage3 => domain.com/new/newpage3
how to do it with .htaccess?
You can use mod_rewrite for stuff like that. Have a look here:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
There are many possibilities and the exact commands depend on your link structure, but in principle (assuming that its about redirecting domain.com/mypageto domain.com/new/mypage) it works like that in your .htaccess:
RewriteEngine On
RewriteRule ^$ /new [L]
Edit: Of course you will have to install / activate Apaches mod_rewrite beforehand if its not already enabled.
This should be fairly simple. Use these 3 rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^oldpage1/?$ /new/newpage1 [L,NC,R=301]
RewriteRule ^oldpage2/?$ /new/newpage2 [L,NC,R=301]
RewriteRule ^oldpage3/?$ /new/newpage3 [L,NC,R=301]

Rewrite 1 level of url directory

I'm looking to rewrite the first directory of a url string and have the rest of the request still work.
Eg: I want it so when a user clicks the link for : /products/category/item.php
it actually grabs the file of : /shop/category/item.php But still shows as /products/category/item.php as the URL
This will be dynamic so it should be something like /products/$ /shop/$1 I'm guessing.
You do not need mod_rewrite. when to avoid mod_rewrite.
Mapping url directories to file directories is a basic functionnality of Apache handled by the mode mod_alias (which is quite certainly already present for you).
So basically you have the Alias and AliasMatch directives. In your case the first one is enough:
Alias /products/ /path/to/web/document/root/shop/
The mapping is done only server-side so the url seen by the end user is never modified.
Try this:
RewriteEngine On
RewriteRule ^products/(.*) shop/$1 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/(.+)$ /shop/$1 [L,NC]

htaccess RewriteRule redirecting for URL's without trailing slashes

I have this snippet in my .htaccess file to defend against anyone trying to get into the app directory.
RewriteCond %{REQUEST_URI} ^/app.*$
RewriteRule ^(.*[^/])/?$ index.php?r=$1 [L,QSA]
And although it works when I go to http://domain/app/, if I make a request to http://domain/app, it redirects to http://domain/app/?r=app.
Does anyone know what needs to be changed to stop such redirection?
Try the DirectorySlash directive that can be use globally or per directory.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
try using
[L]
instead of
[L,QSA]

redirect request uri to mirror site using htaccess

I have a website. I created a mirror of it and uploaded it in a directory called 'mirror'. What I wanted to do is whenever a viewer access for example..
http://www.example.com/this-page/another-segment/?id=1
I want him to be redirected to..
http://www.example.com/mirror/this-page/another-segment/?id=1
^^^^^^
(I am doing this because I want to want to edit my site's design but I don't want viewers to see the changes in progress until they are complete. Thus I want to redirect them to the mirrored snapshot, at least temporarily.)
Please suggest how this can be done using .htaccess or not.
after browsing around the internet. i came up with the idea of putting a /$1, a rewritebase and followsymlinks on the answer givien by appclay
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule (.*) /mirror/$1 [R=301,L]
You'll want to do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule ^/(.*) /mirror/$1 [R]
In your .htaccess file.

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources