RewriteRule for a single .php page in a sub directory - php

I've a joomla website. Recentley I added a subdirectory to the root of the website which contains some pages which are not linked to the CMS in any way.
I wanted to remove the .php from the end of these pages as I was going to promote them over social media and wanted the URL's to be easier for users to remember.
The page in questions is:
http://www.mytestwebsite.com/share/thepage.php
So I added the following rule to my .htaccess file:
RewriteRule ^/share/thepage?$ /share/thepage.php [NC]
With the hopes that the URL would be http://www.mytestwebsite.com/share/thepage
and still load thepage.php but it's not working.

Use:
RewriteEngine on
RewriteRule ^share/thepage/?$ /share/thepage.php [NC,L]
No leading / in the first RewriteRule argument in htaccess.
And /? is for the optional trailing slash (not optional e with e?)

Related

Add URL Rewrite Rule .htaccess

I've installed a Wordpress plugin that makes my website to a static one. The plugin is called "Simply Static".
Now I have a static version of my website here: http://example.com/wp-content/uploads/static/
But my visitors should not see the whole url, i want them just to see http://example.com
or e.g.: http://example.com/contact/ instead of http://example.com/wp-content/uploads/static/contact/
However I can't seem to modify the htaccess to rewrite this.
I've tried:
RewriteEngine On
RewriteRule !^/ /wp-content/uploads/static/%{REQUEST_URI} [L,R=301]
Any ideas appreciated!
To rewrite from / to /wp-content/uploads/static, you just use
RewriteRule !^wp-content/uploads/static /wp-content/uploads/static%{REQUEST_URI} [L]
Note the following
the pattern never starts with a slash in a directory context (e.g. .htaccess)
%{REQUEST_URI} already contains a leading slash
no R|redirect flag

issue with .htaccess url rewriting

I am trying to rewrite url. Below is my code in .htaccess.
RewriteEngine on
RewriteRule ^home?$ index.php
RewriteRule ^contact?$ contact.php
RewriteRule ^about?$ about.php
RewriteRule ^logout?$ logout.php
RewriteRule ^products?$ products.php
RewriteRule ^single-products/([0-9]+)$ single-products.php?product=$1
The pages without parameters works fine. But when I click on the page 'single-products' with parameters it displays like
single-products/1
and images and css styles not worked. When I changed the path css/syle.css to ../css/style.css it works.
Another problem is I can't return to another pages. When I coming to single-products, and click another menu option, url will displays like,
single-products/about
and displays page not found error. I requires /about. My menu link should be
<a href="single-products/1>Product 1</a>
How can I solve these issues. I searched many sites for a solution and didn't find one. Anyone please help me.
Thanks in advance...
For folders to ignore such as CSS etc. try this before your last rewrite rule:
# Directories to ignore
RewriteRule ^(css|images|js)(/.*)?$ - [L]
You might also want to consider using this too, if you plan of having a robots file etc.:
# Ignore favicon & robots.txt
RewriteRule ^favicon\.ico - [L]
RewriteRule ^robots\.txt - [L]
RewriteRule ^sitemap\.xml - [L]
You are missing a closing quote:
Product 1
As for your other question it's hard to answer as you haven't said how you want it to work..
single-products/1
is how the rewite rule is set to work.

How to ignore a folder from beautiful URL using htaccess

I am developing a website, assuming example.com and a blog located at example.com/blog.
The website language is PHP (I designed and programmed it by myself), and I used wordpress as the blog.
I tried to use beautiful URL in main part of the website to convert example.com/services.php to example.com/services by means of this code in htaccess:
# BEAUTIFUL URL
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
The main part of the website works fine. But when I try to load the blog, I must add a trailing slash at the end of the URL to make it work.
http://www.example.com/blog >> 403 Forbidden error
http://www.example.com/blog/ >> Loads correctly
I tried to add trailing slash after URLs using htaccess, But I dont know is this the best solution or not? Could anyone suggest a way to ignore blog folder to be effected by beautiful url?
I simple way to handle this is to create a file called blog.php which simply redirects to example.com/blog/.
RewriteCond %{REQUEST_URI} !^/blog
above the RewriteRules.
Not tested it, I think it should work

Remove in between folder structure from the url in a php website

I have a php website having following folder structure (basic structure).
project_name
app
controller
model
view
css
js
img
index.php
So when I view index.php in WAMP the url is http://localhost/project_name/
But when I go inside the site (eg. login.php which resides under view folder) url is like this. http://localhost/project_name/app/view/login.php
I found that using .htaccess we can change the urls. So I tried this (in .htaccess).
RewriteEngine on
RewriteBase /
Redirect 301 /project_name/app/view/login.php /project_name/login.php
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
Now url is http://localhost/project_name/login.php It is correct. But it seems php does not use the original link to grab the file (ie. from /project_name/app/view/login.php) but from here /project_name/login.php
So it throws 404 error.
What should I change? Please help me, i am just trying to hide /app/view/ part from the url so that user won't see my folder structure. I have read about various ways of doing that for about 9hrs today but still couldn't get anything working correctly.
Hope my question is clear enough. Any help is greatly appreciated!
URI's passed through rewrite rules in an htaccess file has the leading slash removed, so your rule:
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
won't ever match anything because of ^/. Also, since you are rewriting to a URI that matches a previous redirect, your browser will see a redirect loop and say that it's not redirecting properly. You'll need to match against the actual request and not the URI:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project_name/app/view/login\.php
RewriteRule ^ /project_name/login.php [L,R=301]
RewriteRule ^/?project_name/login.php$ /project_name/app/view/login.php [L]

Mod-Rewrite and .htaccess Problems

Having some issues with Mod_Rewrite. I must admit this is my first venture into the module so I could be going through something silly. I have been trying to get this to work on my own for a day or so off and on and I can not figure out why this is not working.
I am getting the following errors:
Notice: Undefined index: Dir in D:\wamp\www\WildForFashion\Portal\Index.php on line 31
What I am trying to accomplish is to be able to have one template page (index.php) and have Mod_Rewrite change the url for me so it can be Search Friendly and to also ease coding and design.
I was also having issues when it came to no Images / CSS files loading at all, I did read that by using an absolute url would work.
As of right now I am working in the PORTAL directory first (which is an admin page for website maintenance) and then will be moving onto the root directory. I am not sure if I need to have one .htaccess file or two (one being in root dir, second being in portal dir).
The DIR folder is to hold each category and page for the website like the following:
Domain.com/{CATAGORY}/{PAGE}/
ROOT / DIR / {CATAGORY} / {Page}.php
Domain.com/Portal/{CATAGORY}/{PAGE}/
ROOT / Portal / DIR / {CATAGORY} / {Page}.php
.htaccess Before
RewriteEngine on
RewriteRule ^/Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule ^Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule ^Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
.htaccess After (Working w/ Exceptions)
RewriteEngine on
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
RewriteRule Portal/([^/]+)/([^/]+)/$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
Index.php
<?php
include_once("DIR/" . $_GET['Dir'] . "/" . $_GET['Page'] . ".php");
?>
Directory Tree
CSS
DIR
IMG
JS
SRC
Portal
CSS
DIR
Portal
Inventory
Stats
Orders
IMG
JS
SRC
Index.php
.htaccess
Index.php
You are including forward slashes in the matching rule that don't need to be there and excluding them from the redirect path where they do need to be. Try this:
RewriteRule Portal/$ Portal/Index.php?Dir=Portal&Page=Home [L,QSA]
RewriteRule Portal/(.*)/(.*)$ Portal/Index.php?Dir=$1&Page=$2 [L,QSA]
RewriteRule Portal/(.*)/$ Portal/Index.php?Dir=$1&Page=Home [L,QSA]
Note I removed the ^ from the matching portions of the rewrite rules as this indicated taht should be the beginning of the URI. I also removed the "/" in front of the substritution pattern.

Categories