htaccess rewrite url to remove the /folder/ but leave everything else after - php

I am trying to use htaccess to remove the sub directory from the url and leave everything else.
The current links look like this...
http://blog.domain.com/blog/page-title
I need the links to look like this...
http://blog.domain.com/page-title
there is an installation of WP at both locations with the same DB (different physical databases)
so I have tried this...
RewriteEngine On
RewriteRule ^$ blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blog/$1
and lots of other things, just cant seem to work it out with all the attempts.
Would love a little help on this

How about
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ blog/$1 [L]
This will make every links like:
http://blog.domain.com/page-title
behave as if they were:
http://blog.domain.com/blog/page-title
And if you want the inverse effect, meaning that all link with /blog/stuff change into /stuff try this instead:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ /$1 [L,R=301]

Related

Cut folder path from URL via .htaccess

I'm trying to shorten my URL but sadly can't find anything that helps.
I divide my code in folders. Index is positioned at root just like my .htaccess.
The folders are named like the file extensions, so php, js, css [...]
I have a link like the following:
localhost/php/getBets.php
and want it to be
localhost/getBets/
I already have the part that cut's the .php extension at the end, so here is my full .htacces
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Hide Index
IndexIgnore *
# Forbid accessing certain sites
RedirectMatch 403 ^/.gitignore$
RedirectMatch 403 ^/.htaccess$
RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC]
# Hide .php file ending in URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can someone maybe tell me how I could achieve this? :)
Thanks alot!
For your required url you can use below rule in root directory it is for rewriting,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
Use %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]

using slash in htaccess makes css files not to load

first, i'm trying to find an answer to this like 3 nights :) If there's answer of this, i couldn't find or understand.
My question is simple:
I want to split get parameters with slashes. And use this globally in my script.
in htaccess, i wrote this;
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
but this blocks (or sth like that) css files. when browser loads page, css file links looks normal but browser can't reach them (because of htaccess gets directories as parameters)
Thank you for your helps
edit: my full .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
Make sure both your rules look like this. Conditions only work for the first rewrite rule following them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
Also use absolute paths for your CSS files or put a / before the file path.
By putting this above the rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It checks if the files already exist (eg the CSS files) and therefore won't try to rewrite or redirect them.

two RewriteRule's for two different but like urls

My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations

issue with URL redirection in Apache (mod_rewrite / mod_alias)

There are many issues related to Apache configuration with mod_rewrite and mod_alias on Stack Exchange but I couldn't find an answer to my issue, so here is another question! ;)
I migrated an old website to a new location.
Articles used to be accessible to an URL such as http://xxx/blog/index.php?post/YYYY/MM/DD/title, so there are many links of that form through the existing webpages.
Now, the URL should be http://xxx/post/YYYY/MM/DD/title, so just remove blog/index.php? from the final URL.
I wanted to use mod_alias and a Redirect clause, but I read here that the Redirect clause was interpreted after the RewriteRule clause, which is a problem in my case because I'm already using a RewriteRule condition.
Here is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I tried to modify it this way:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/index.php? / [R=301,L]
RewriteRule ^(.*)$ index.php?$1
It almost works except:
http://xxx/blog/index.php?post/YYYY/MM/DD/title is redirected/rewritten to http://xxx/blog/?post/YYYY/MM/DD/title (note the ?) so it doesn't work
it looks like this new line messes up a lot of other URLs that do not start with /blog/index.php? such as the backend URL...
Any help will be more than welcome!
Edit (2014-07-16):
If I use the following rule:
RewriteRule ^/?blog/index\.php(.*)$ /$1 [R=301,L]
then going to
http://xxx/blog/index.php?test
takes me to
http://xxx/?test
which is almost correct (the interrogation mark is still a problem)!
But when I try to match the interrogation mark by adding \?:
RewriteRule ^/?blog/index\.php\?(.*)$ /$1 [R=301,L]
then it just stops working... Going there:
http://xxx/blog/index.php?test
just leaves me there!
Why is that?
Try adding this rule to the beginning of your set of rules:
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
so that it looks like:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Removing '&title' from the URL in .htaccess

I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]

Categories