Is this .htaccess rewrite even possible? - .htaccess

My co-worker is proposing a .htaccess rewrite rule to implement on our system. I'm not even sure if it's possible, and I want someone more educated than me on the subject to verify.
Here's the URL currently:
http://cms.phasesolutions.ca/themes/Default/page.php?slug=home
Here's how he wants it to show up:
http://cms.phasesolutions.ca/pages/home/
Is that possible? (where the variable for "?slug=", and the "Default" folder, are subject to change, depending which theme they're accessing and what slug they're specifying)
Let me know,
thanks

Posting a solution in case you need starting point.
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
# handles /Default/home/ to /themes/Default/page.php?slug=home
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /themes/$1/$2.php?slug=home [L,QSA]

Related

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]

Remove folder with HTACCESS

When users go to the following url:
http://mysite.com/folder/subfolder?a=blah&b=blah
I need to point them to content that is in the root folder while keeping the variables intact:
http://mysite.com/?a=blah&b=blah
Since http://mysite.com is a virtual host, this solution should also leave other base URLs alone.
I'd prefer not to do a full redirect because we use log tracking in our system and we need to have the headers return a 200 code for the logs (maybe this doesn't matter).
I know that this is a fairly straightforward question for someone who really understands .htaccess redirects. Thanks in advance!
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 %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^folder/subfolder/?$ / [L,NC]

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.

Remove file extensions using htaccess in subdirectories

I'm trying to remove file extensions with htaccess, so www.mysite.com/file.php becomes www.mysite.com/file.
I'm using the following code in the .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So far, so good. Where it falls down is in subfolders: www.mysite.com/subfolder/file.php
becomes www.mysite.com/file.
I've tried uploading another .htaccess file in the subfolder, but it still does the same. It feels like it should be really simple, but I'm struggling...can anyone help me out? Thanks!
Edit Sorry folks, should have said - the file is in a subfolder like so:
www.mysite.com/folder/subfolder/file.php
The .htaccess file is in /folder, the URL changes to this format:
www.mysite.com/subfolder/file
Apologies for misleading.
This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
You shouldn't be using rewrite rules for this. Apache has built-in option explicitly for doing what you're trying to do, called MultiViews, and that's what you should be using:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
Just add Options MultiViews to your .htaccess, and make sure that AllowOverride is properly configured.
A guess in the wild
Try
RewriteRule ^(.+)$ $1.php [NC,L]

PHP page access without extension and variable

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]

Resources