How to ignore a folder from beautiful URL using htaccess - php

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

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

RewriteRule for a single .php page in a sub directory

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?)

htaccess redirect root to subdirectory but allow index.php in root to function

This is our situation: The original domain name (say, www.example.com) was done in WordPress and installed in the root. We have created a new website using Joomla and placed it in a sub-directory (say, www.example.com/PORTALsite).
I have been able to successfully redirect the root (www.example.com/) to this sub-directory (www.example.com/PORTALsite) using this code in htaccess:
RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
This works good, EXCEPT that we need the original WordPress website (the Home page located at www.example.com/index.php) to still function. We need this, because we have hundreds of pages within this WordPress site that are indexed in the Search Engines and we want all of that old content to continue to function.
The code above works well for all files and sub-directories within the Root, allowing our old content to continue to function, and it correctly redirects the root itself (www.example.com/) to the new Joomla website's sub-directory (www.example.com/PORTALsite).
HOWEVER, the problem is this: We still need the old WordPress site's index.php file to function if it is accessed directly (i.e., if someone types in www.example.com/index.php), BUT it is NOT functioning. If you go to www.example.com/index.php is also redirects to www.example.com/PORTALsite. Though all of the other files in the root still function as we desired them to.
Let me make sure I am explaining this properly: If someone goes our domain (www.example.com) it redirects as we want to the new Joomla website (at www.example.com/PORTALsite). BUT, if you go to www.example.com/index.php (the old WordPress site's Home page) it also redirects to the new Joomla website's sub-directory (www.example.com/PORTALsite). We do Not want this to happen. We want it so that if the root's index.php file is accessed directly, that it will still function (to keep all the old WordPress content intact).
We ONLY want requests to the domain name itself, www.example.com, to redirect to www.example.com/PORTALsite. But, we need all of the other files and sub-directories, including www.example.com/index.php, to still function (so that people can still access all of the old WordPress website's content, as well as numerous static pages).
All the files and sub-directories in the root (www.example.com/) still function as we want, BUT just the old WordPress site's Home page (located at www.example.com/index.php) also redirects to the new site at www.example.com/PORTALsite (along with requests for the domain name itself www.example.com). So the code above is VERY close to working as we desire.
I greatly appreciate the expertise here on StackOverflow and greatly appreciate any help we can receive. Thank you in advance!
UPDATE: This is the code in our HTACCESS file: (used "example.com" rather than true domain name)
RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
In light of anubhava responses below, his code should work once I get WordPress to stop removing the "index.php" from the URL. Thank you anubhava! Here's the whole file.
Replace this rule:
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[\s?]
RewriteRule ^$ /PORTALsite [R=301,L]
Make sure this is first rule in your wordpress .htaccess
Make sure to test in a different browser to avoid 301 caching issues
Change permalink setting of WP to make your BLOG/Home URLs as http://domain.com/index.php instead of http://domain.com/

Apache htaccess rewriterule issue

I have the following rule in htaccess
RewriteRule ^noticias/?$ /index.php?view=pagination&ptype=noticias [L]
But the problem is that if I put some spaces in the URL, like this .. I can access the page normally.
http://gamesite.org/noticias /
I do not know what I do to block it.
without / the browser removes the spaces. but the problem is that I need this bar, because of paging on my site. Ex: /noticias/1/
Here is my htaccess file http://pastebin.com/gwtZGaGv

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]

Categories