Apache Mod-Rewrite Question - apache

I have a PHP scripted named index.php inside a folder named blog. There are three different views.
http://www.myDomain.com/blog/index.php
http://www.myDomain.com/blog/index.php?tags=list of categories
http://www.myDomain.com/blog/index.php?post=name of post
I would like to change the view based on the URL.
/blog redirects to number 1 above
/blog/name-of-category redirects to numbe 2 above
/blog/name-of-category/name-of-post redirects to number 3 above.
Right now I have the following mod_rewrite rules.
RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]
This does not work, and I'm not sure why. Right now it always redirects to the last URL:
blog/index.php?tags=$1
And the GET data contains "index.php."
Also, if add a forward slash to the final rule like so:
RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]
All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.
Any ideas what's happening here? how I can fix this?
Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:
<?php
define ('BASE_PATH', "../blog/");
include_once(BASE_PATH . 'controller/Controller.php');
$controller = new Controller();
$controller->invoke();
See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.

In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.

You could write it as follows.
RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]

Related

Create a 301 redirect excluding a subdirectory [duplicate]

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]

Rewrite pages with a certain url to another htaccess

What I need to do it rewrite a url from one thing to another eg:
http://www.domain.com/page_one/blah //to
http://www.domain.com/page_two/blah
I've tried a few script I've found around the internet but I'm terrible with .htaccess and can never understand or get it right.
If you want to change only this specific url, use this:
RewriteEngine On
RewriteRule ^page_one/blah?$ http://www.domain.com/page_two/bla [L]
If you want to change the url for every file into the "page_one"-folder, this will help you:
RewriteEngine On
RewriteRule ^page_one/([^/]+)$ http://www.domain.com/page_two/$1 [L]
The RewriteEngine On activates the RewriteEnigine, so that you are able to use RewriteRules.
The ([^/]+) in the second solution means "every file, but no folders (the slash is excluded)". This is stored in $1 and used to create the new url.
Edit
[L] stops the script from using the other rules (if you have some)

trying to get mod_rewrite to work

I have this problem where I switched CMS from the old one to the new one so I am trying to redirect urls properly.
Here are some example urls I wanted to convert:
#-1) http://www.mysite.com/?dispatch=search_data&features=hash_tag
#-2) http://www.mysite.com/index.php?dispatch=search_data&features=hash_tag
Basically both of them are identical except the first one doesn't have index.php in the url.
I got the following to code to partially work:
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^$ http://www.mysite.com/? [L,R=301]
The above code works for #-1 (when there is no index.php) but when there is index.php it doesn't work.
Any help / guidance is much appreciated.
Thanks
____________UPDATE_______________
As per request, here is what the the target url should be:
http://www.mysite.com/
What do you mean it "works"?
You should probably post an example of what you want the new url to look like given the old url.
But in any case, part of your problem is that the RewriteRule line is specifically excluding index.php and any other page on your site.
What you wrote basically tranlates to this:
if the query string contains "dispatch=...":
redirect requests FOR THE HOME PAGE ONLY (^$) to http://www.mysite.com/
RewriteCond %{QUERY_STRING} dispatch=(.*)
RewriteRule ^(index.php)?$ http://www.mysite.com/? [L,R=301]
have you tried changing the second part to include the "index.php" part?
RewriteRule ^$ http://www.mysite.com/index.php? [L,R=301]

Redirect site with .htaccess but exclude one folder

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.