htaccess mod_rewrite RewriteRule - apache

I am trying to set up an .htaccess file to convert an incoming link like:
http://domain.com/root/TopNav/SubNav/SEO-friendly-file-name-p#
into this:
http://domain.com/root/index.php?t=TopNav&s=SubNav&l=SEO-friendly-file-name&p=#
where p# is the page id and TopNav/SubNav represent the navigation menu path to the file
I have been able to get it to work in all cases except for when there are arguments after the .php (it does the mod rewrite, but loses the parameters). Originally, I was hoping to have the .htaccess parse the url string so that it was ready for the script to use, but at this point I would be happy with any solution that takes the incoming url and dumps it as a string onto root/index.php.
here's what I currently have in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)/(.*)\.php index.php?s=$1&p=$2 [L]
RewriteRule ^(.*)/ index.php?p=$1&s=$1 [L]
</IfModule>
ErrorDocument 404 /404.php
Any thoughts on what I might be doing wrong? Suggestions of a better way to get this done?
Thanks
** someone suggested changing the [L] to [L,QSA] and that seems to have worked. Thanks, whoever suggested that...

With a URU that looks like this: /TopNav/SubNav/SEO-friendly-file-name-p# you've got 4 groupings you need:
TopNav
SubNav
SEO-friendly-file-name
#
So you need to craft your regex so that it captures these 4 things in one go.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.+?)-p([0-9]+)$ index.php?t=$1&s=$2&l=$3&p=$4 [L,QSA]
This would go in the htaccess file in your /root directory

Related

Change root directory in .htaccess

I have a problem with changing the root directory in .htaccess.
My folder structure looks like this.
What I want to achieve is, when I visit this page:
/comparty/about/
The page I will see is this page:
/comparty/pages/about/
I have already tried to search on Google, but the code I found did not work, though I tried to change it:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^(.*)$ pages/$1 [L]
I don't want it to redirect, I want to keep the same URL. Also I've had a big problem with Apache caching the .htaccess file, so I haven't been able to test many things.
Thanks in advance.
EDIT
I found a way to rewrite the URL from /comparty/pages/about/ to /comparty/about/ - this is the code:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^about/(.*)$ pages/$1 [L]
This only works on the about page, though. What would I have to do, to make it dynamic and work with every page?
You need to use a dynmic pattern :
RewriteEngine On
RewriteBase /comparty/
#if the request is not for an existent dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is not for an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite the request to "/pages/request"
RewriteRule ^(.*)$ pages/$1 [L]
RewriteConditions above are important to avoid rewriting your existent files and directories to the /pages subfolder. Without those conditionrt the Rule will rewrite all requests including the destination path /pages and this may result in rewrite loop error.

Using .HTACCESS Rewrite Rule

I'm trying to rewrite URLs for my dynamically generated PHP site.
I load new templates into index.php by using the following GET:
localhost/dmk/?req=signin
localhost/dmk/?req=useraccount
I want these links to appear as:
localhost/dmk/signin
localhost/dmk/useraccount
But for the life of me I cannot figure out how to do this. Everything I try either produces a 500 Internal Server Error, or has no effect at all.
I must be missing the point of RewriteRule.
You should read some documentation in this direction. I know it's a bit frustrating at first to write the rules, but it gets easier. You need to learn regular expressions to write the rules (you can start here: http://www.regular-expressions.info/)
As for the rules you need, they go like this:
RewriteEngine On
RewriteRule ^signin$ index.php?req=signin [L,QSA]
RewriteRule ^useraccount$ index.php?req=useraccount [L,QSA]
or
RewriteRule ^(signin|useraccount)$ index.php?res=$1 [L,QSA]
You can paste the rules you have used, maybe someone will explain you what you did wrong.
Try this
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^dmk/(.+)$ dmk/?req=$1 [NC,QSA,L]
This would redirect any URL like /dmk/page that does not conflict with an existing file or directory to /dmk/?req=page. I'm assuming your index.php is in /dmk directory.

mod_rewrite with multiple query strings?

I'm trying to cleanup some URLs on my blog, so I've decided to look into mod_rewrite. I haven't a clue what I'm doing though, so I was hoping I could get some help :P I have links like http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4. Although it works, and people still get the content I want them to have, I don't like them having to look at all the query strings. I want to turn the above link into http://kn3rdmeister.com/blog/2012/07/04/4.php.
This is what my .htaccess looks like right now.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^y=([0-9){4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^/blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]
Like I said, I'm absolutely clueless :D
If you're using apache 2.0 or higher, you're going to need to remove the leading slash (the prefix) if these rules are in an .htaccess file, so that your regular expression looks like this:
# also note this needs to be a "]"--v
RewriteCond %{QUERY_STRING} ^y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]
This is going to make it so when someone puts http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4 in their browser's URL address bar, their browser will get redirected to http://kn3rdmeister.com/blog/2012/07/04/4.php and the new URL will appear in their address bar.
I assume you've got something setup on your server to handle a request like blog/2012/07/04/4.php.
At first you should define your URLs!!!
Like:
/blog shows front page
/blog/1234 shows post 1234
/blog/date/2012 shows posts by year
/blog/date/2012/06 shows posts by year and month
/blog/date/2012/06/01 shows posts by year and month and day
and so on...
First option is to rewrite each of your defined URLs to index.php. Your index.php has only to handle the submitted GET parameters.
### Do only if rewrite is installed
<IfModule mod_rewrite.c>
### Start rewrite and set basedir
RewriteEngine on
RewriteBase /
### Rewrite only if no file link or dir exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
### Rewrite frontpage
RewriteRule ^blog$ /index.php?action=showfront [L,QSA]
### Rewrite post
RewriteRule ^blog/([0-9]+)$ /index.php?action=showpost_by_id&id=$1 [L,QSA]
### Rewrite posts by date
RewriteRule ^blog/date/([0-9]{4})$ /index.php?action=showposts_by_date&year=$1 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2&day=$3 [L,QSA]
### Rewrite posts by tag
RewriteRule ^blog/tag/([a-zA-Z0-9_-]+)$ /index.php?action=showposts_by_tag&tag=$1 [L,QSA]
</IfModule>
Test in index.php with:
print_r($_GET);
print_r($_POST);
The second option is to rewrite all URLs and your index.php needs to handle all possible URLs. So at first it needs something like a router that splits the incoming URL in parts and then send the requested page or an error-page. I would try this at first as the bloody school.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
</IfModule>
Test in index.php with:
print_r(explode('/', ltrim($_SERVER['PATH_INFO'], '/')));
print_r($_GET);
print_r($_POST);
The third option is to use a PHP framework. A framework may help you to write your code quite fast. It delivers you many base-classes like a router. (f.e. ZendFramework, Flow3, Kohana, Symfony, CodeIgniter, CakePHP, yii and others). This will make you more advanced.
The fourth and laziest option is to use a ready made software like Wordpress.

Url rewriting mod_rewrite

I have the url such as:
page.com/content.php?xname=p&yname=q&zid=1
I want to rewrite this url using apache mod_rewrite into something like:
page.com/p/q/
note there should not be 'zid' parameter in renamed url. I know expressions are passed as GET into the original url.
Is it possible to rename as above. If yes, How to achieve this?
This one works fine for me and will rewrite request for /p/q/ to /content.php?xname=p&yname=q&zid=1.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ content.php?xname=$1&yname=$2&zid=1 [QSA,L]
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
It will not rewrite if requested URL is a real file or folder (I'm sure you do not want to rewrite images or some other pages -- I had to add such condition since I do not know what is your website structure is).
RewriteRule ^content\.php\?xname=(p)&yname=(q)&zid=1$ /$1/$2 [R]
Instead of p and q you can try expressions like [a-Z0-9_-]+ to match identifiers.
There's an online testing tool here: http://civilolydnad.se/projects/rewriterule/
RewriteEngine On
RewriteRule ^/[a-z0-9]+/[a-z0-9]+/$ content.php?xname=$1&yname=$2 [L]

How do I ignore a directory in mod_rewrite?

I'm trying to have the modrewrite rules skip the directory vip. I've tried a number of things as you can see below, but to no avail.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteRule ^vip$ - [PT]
RewriteRule ^vip/.$ - [PT]
#RewriteCond %{REQUEST_URI} !/vip
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How do I get modrewrite to entirely ignore the /vip/ directory so that all requests pass directly to the folder?
Update:
As points of clarity:
It's hosted on Dreamhost
The folders are within a wordpress directory
the /vip/ folder contains a webdav .htaccess etc (though I dont think this is important
Try putting this before any other rules.
RewriteRule ^vip - [L,NC]
It will match any URI beginning vip.
The - means do nothing.
The L means this should be last rule; ignore everything following.
The NC means no-case (so "VIP" is also matched).
Note that it matches anything beginning vip. The expression ^vip$ would match vip but not vip/ or vip/index.html. The $ may have been your downfall. If you really want to do it right, you might want to go with ^vip(/|$) so you don't match vip-page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This says if it's an existing file or a directory don't touch it. You should be able to access site.com/vip and no rewrite rule should take place.
The code you are adding, and all answers that are providing Rewrite rules/conditions are useless! The default WordPress code already does everything that you should need it to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Those lines say "if it's NOT an existing file (-f) or directory (-d), pass it along to WordPress. Adding additional rules, not matter how specific or good they are, is redundant--you should already be covered by the WordPress rules!
So why aren't they working???
The .htaccess in the vip directory is throwing an error. The exact same thing happens if you password protect a directory.
Here is the solution:
ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt
Insert those lines before the WordPress code, and then create /err.txt. This way, when it comes upon your WebDAV (or password protected directory) and fails, it will go to that file, and get caught by the existing default WordPress condition (RewriteCond %{REQUEST_FILENAME} !-f).
In summary, the final solution is:
ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I posted more about the cause of this problem in my specific situation, involving Wordpress and WebDAV on Dreamhost, which I expect many others to be having on my site.
You mentioned you already have a .htaccess file in the directory you want to ignore - you can use
RewriteEngine off
In that .htaccess to stop use of mod_rewrite (not sure if you're using mod_rewrite in that folder, if you are then that won't help since you can't turn it off).
Try replacing this part of your code:
RewriteRule ^vip/.$ - [PT]
...with the following:
RewriteCond %{REQUEST_URI} !(vip) [NC]
That should fix things up.
RewriteCond %{REQUEST_URI} !^pilot/
is the way to do that.
In my case, the answer by brentonstrine (and I see matdumsa also had the same idea) was the right one... I wanted to up-vote their answers, but being new here, I have no "reputation", so I have to write a full answer, in order to emphasize what I think is the real key here.
Several of these answers would successfully stop the WordPress index.php from being used ... but in many cases, the reason for doing this is that there is a real directory with real pages in it that you want to display directly, and the
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
lines already take care of that, so most of those solutions are a distraction in a case like mine.
The key was brentonstrine's insight that the error was a secondary effect, caused by the password-protection inside the directory I was trying to display directly. By putting in the
ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt
lines and creating error pages (I actually created err401.html and err403.html and made more informative error messages) I stopped the 404 response being generated when it couldn't find any page to display for 401 Authentication Required, and then the folder worked as expected... showing an apache login dialog, then the contents of the folder, or on failure, my error 401 page.
I’ve had the same issue using wordpress and found that the issue is linked with not having proper handler for 401 and 403 errors..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These conditions are already supposed not to rewrite the url of existing folders but they don’t do their job for password protected folders. In my case, adding the following two lines to my root .htaccess fixed the problem:
ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html
Of course you need to create the /misc/myerror.html,
This works ...
RewriteRule ^vip - [L,NC]
But ensure it is the first rule after
RewriteEngine on
i.e.
ErrorDocument 404 /page-not-found.html
RewriteEngine on
RewriteRule ^vip - [L,NC]
AddType application/x-httpd-php .html .htm
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
etc
I'm not sure if I understand your objective, but the following might do what you're after?
RewriteRule ^/vip/(.*)$ /$1?%{QUERY_STRING} [L]
This will cause a URL such as http://www.example.com/vip/fred.html to be rewritten without the /vip.