htaccess URL Rewrite - Root to Subfolder (with folder of the same name) - regex

I have a file at www.domain.com/blog/blog.html that I want to rewrite as www.domain.com/blog.
I have figured out the rewrite for removing the extension .html but am having either circular issues or end up pointing the blog rewrite to the blog folder when trying to do the above.
I am new to rewrite syntax, so help would be much appreciated!

Create /blog/.htaccess if it doesn't already exist and place this rule:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^/?$ blog.html [L]
# To internally forward /blog/file to /blog/file.html
RewriteCond %{DOCUMENT_ROOT}/blog/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

Related

Clean URL using regex and .htaccess & mod_rewrite

I am using below code on my .htaccess file
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
the goal is to redirect a clean URL like below
http://www.zire20.ir/77438/9512
to this one
http://www.zire20.ir/view_basket.php?order_id=77438&pin=9512
The thing is it was working on my previous server but now I changed to godaddy hosting and it's not working! any idea ?
p.s:
and my whole .htaccess file is like below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^zire20.ir [NC]
RewriteRule ^(.*)$ http://www.zire20.ir/$1 [L,R=301]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /view_basket.php?order_id=$1&pin=$2 [L]
lots of photos are not loading!
The problem with your current rule is that you are rewriting unconditionally. Any URL that contains a single slash will get rewritten. I imagine that some of your (static) photo URLs match this pattern.
Common practise is to only rewrite the URL if it doesn't match an existing file (or directory):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /view_basket.php?order_id=$1&pin=$2 [L]
This makes sure the request is only rewritten for non-existing files (not a file or a directory). I've also made the pattern a little more restrictive so there must be 1 or more chars before and after the slash (+), instead of 0 or more (*).
The thing is it was working on my previous server
I can't see how this was possible, unless the URL structure was different on the previous server?

Remove last Path from URL using .htaccess

I need to create a RewriteRule on my .htaccess file which removes the Path ("page-2") if it's included at the end of the URL. So for example:
http://myhost.com/path/page-2
should redirect to:
http://myhost.com/path
I've found a similar solution on SO:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /$1/page-2/ [L]
However it does not work for me. No redirection happens.
Any help?
You need to have this rule:
RewriteEngine On
RewriteRule ^(.+?)/page-2/?$ /$1 [L,NC,R=301]

RewriteRule for htaccess in root of site

I want to rewrite all URLs to a certain file (/blog/post.php) within certain folders (/blog) except a couple of files (/blog/post.php of course and the root of the directory /blog/index.php or /blog/). I therefore have this:
RewriteRule ^blog/index.php - [L]
RewriteRule ^blog/post.php - [L]
RewriteRule ^blog/(.*) /blog/post.php [L,QSA]
Where the intended result is that all URLs except index.php and post.php get rewritten to post.php
The problem is if I put this in the .htaccess in the site root, when you type just website.com/blog/ it rewrites to page to post.php whereas website.com/blog/index.php doesn't.
If I put the same code in the .htaccess file in the blog folder (but without the word blog in each line) it works as expected/wanted.
I'm very sorry for another RewriteRule question but its a specific example/problem I've not been able to find and I would be very grateful if anyone had any tips.
Have your single rule like this:
RewriteCond %{REQUEST_URI} !/blog/(index|post)\.php$ [NC]
RewriteRule ^blog/(.+)$ /blog/post.php [L]

Rewrite url for subdirectory in .htaccess

I moved wordpress files from root to a subdirectory. There are some files uploaded in wp-content/uploads folder. When accessing those files directly, I get a 404 error.
I need to rewrite the url only for the wp-content/uploads through .htaccess
What I need is to redirect from
http://example.com/wp-content/uploads/2012/09/report.pdf
to
http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf
This is what I've tried
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
This adds a subdirectory wp_sub to every link.
I need to add a subdirectory "wp_sub" only for wp-content/uploads links.
What am I missing?
Let's see what
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1
means:
You rewrite everything (^(.*)$) to http://www.example.com/wp_sub/$1.
Well, you don't want to rewrite everything, do you? :)
So we have to restrict this to only affect the wp-content folder:
RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
Place this rule in your /wp-content/uploads/.htaccess file:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^(.*)$ /wp_sub/$1 [L,R]
You can use the code :
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L]

Apache .htacces Rewrite Rule to Remove .php File Extensions

The title explains the basics, now, here comes the problem:
I have many files that look like this:
<?php include 'mynavbar.php';
include 'myheader.php';
include 'myfirstline.php';
include 'mybuttons.php';
include 'myadvert.php';
include 'myimg.php';?>
And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending
Following code should work for you in .htaccess file to hide .php extension:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';
It is because those includes are processed by php itself and don't go through Apache.
The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.
# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((.*/)*[^./]+)/*$ $1.php [L]
This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.
You are not taking the correct path for fixing the problem you trying to solve.
If I understood well, what you want to do is having an url like :
http://example.com/my/url
instead of (for example):
http://example.com/my/url.php
If this is the case, what you should look for is rewrite url