Redirecting with htaccess using similar rules - .htaccess

I'm using .htaccess to rewrite my URLs and so far it's almost working as it should...
The problem I'm having is that mysite.com/author and mysite.com/author/submit/1 are both redirecting to the same page (mysite.com/author.)
These are the rewrite rules that I'm currently using:
RewriteRule ^author /zabjournal/pages/author/active_submissions.php [L]
RewriteRule ^author/submit/1 /zabjournal/pages/author/submit_step1.php [L]
How do I get the second rule to work?

This is because you have put the rules in the wrong order.
The first rule validates and executes because author is the first string in both URLs.
/author
/author/submit/1
/author/blah
/author/blah/blah/blah/blah/blah
The URLs above will all match the first rule, so, therefore, it will be executed.
The [L] (which stands for last) at the end of a rule means that it won't process other rules if that rule is executed.
But, if you change the order of your rewrite rules it will first check to see if the URL matches /author/submit/1 and, if it does, it will execute that rewrite and then stop; but if it doesn't, it will continue to the next rule, which, in your example, would be /author.

Related

.htaccess: Multiple TLD's to 1 TLD excluding subdomains

I have multiple TLD's (domainX.com, domainY.net, ...) pointing towards the same folder. In this folder I would like to add a .htaccess file to redirect ALL www and non-www URL's that aren't domainY.com to domainY.com.
There is one twist here however. I have some subdomains: alfa.domainY.com and beta.domainY.com and gamma.domainY.com set-up, which in all my tests keep redirecting to domainY.com.
Any chance anyone can give me a successful bit of code here?
EDIT: Maybe also add some #Comments, I noticed most answers here lack that, and I think it means some of them can't be reused as people don't know what they do. I can also add this myself afterwards.
Try adding these rules to the htaccess file of your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !domainY\.net$ [NC]
RewriteRule ^(.*)$ http://domainY.net/$1 [L,R=301]
The expression matching the host will fail for any alpha.domainY.net because it only matches against the TLD (.net) and domain (domainY).
The first line turns on the rewrite engine.
The second line, the condition, is a true/false expression that is applied to the immediately following rule. In this case, it checks the request's Host: header, and if it ends with domainY.net, then the condition fails, because of the ! in front.
The third line is the rule, the URI is used to match against the pattern ^(.*)$ which essentially matches everything, and is captured via the parentheses. Then the next bit is the target. If the rule matches, which it does because the pattern matches everything, then the target is applied, and in this case, it redirects the browser to domainY.net and passes the same URI along with it via the regex backreference $1.

mod_rewrite for a 301 redirect. Not redirecting to proper location

I'm trying to use a RewriteRule (using ISAPI, NOT on an Apache server) to 301 redirect a url such as:
http://www.mydomain.com/news/story-title/
to
http://www.mydomain.com/news/detail/story-title/
What I've gotten so far is:
RewriteRule ^news/(?!detail)/?$ news/detail/$1/ [L,R=301]
which successfully ignores urls that already have the "detail" in them (in some of my first attempts I ended up with a loop and a url like "/news/detail/detail/detail..."), but visiting /news/story-title/ gives me a 404 so it's not redirecting to the proper location.
Change your rewrite rule to
RewriteRule ^news/(?!detail)([^/]+)/?$ news/detail/$1/ [L,R=301]
EDIT : (How it works?)
/(?!detail) is a negative lookahead but it's also non-capturing i.e. it matches / but not what comes after it; just makes sure that it isn't "detail". So, I added a capturing group ([^/]+) to capure those characters (one or more + of anything that's not a/) optionally ending with a /.
Hence, the $1 now gets replaced with the matched directory name.

htaccess help to rename directory

I have 2 directories that I would like to rename without losing my search ranking. Here is the url convention and suggested name change:
Old: mysitename.com/folder1/folder2/filenames
to
New: mysitename.com/newfoldername1/newfoldername2/filenames
filename is the given name of each page
RewriteRule ^(.*)/folder1/folder2/(.*)$ $1/newfoldername1/newfoldername2/$2 [R,L]
The problem with this rule is that RewriteRules only match on the path, not the domain. You have the (.*)/ at the front of the rule as if you are trying to match the domain, which isn't necessary. You have also set it as a redirect rather than a rewrite ... maybe this is what you want, in which case keep the "R" flag as you had it, but I'll remove it in the code below in case you just want to rewrite. Modify the rule as follows and it should work:
RewriteRule ^newfoldername1/newfoldername2/(.*)$ /folder1/folder2/$1
Edit: Now that you've posted your other rules, I removed the [L] and reversed the order to what I think you're trying to do.
Edit 2: The above rule assumed that you wanted a user entering domain.com/newfoldername1/newfoldername2/whatever to be silently rewritten to domain.com/folder1/folder2/whatever. The phrasing of your latest comment indicates that instead you want a user entering domain.com/folder1/folder2/whatever to be redirected (in other words, to have the address change in their browser) to domain.com/newfoldername1/newfoldername2/whatever and that the server is ready to process this new path. In that case, the following rule is required:
RewriteRule ^folder1/folder2(/.*)?$ /newfoldername1/newfoldername2$1 [R=301,L]

ISAPI Rewrite Help With Redirect To Sub Domain

I need a bit of ISAPI syntax help, I am about to put live a new site and want to archive the old forum onto an archive sub domain.
The forum is in ASP and currently has this URL
http://www.mywebsite.co.uk/forum/forum_posts.asp?TID=34419
http://www.mywebsite.co.uk/forum/forum_topics.asp?FID=47
I want every request for the forum to be 301 redirected to:
http://archive.mywebsite.co.uk/forum/forum_posts.asp?TID=34419
http://archive.mywebsite.co.uk/forum/forum_topics.asp?FID=47
Basically anything with in the forum folder with .asp extension with or without a query string - Any help greatly appreciated.
Thanks
For redirecting a url to a subdomain, it has been a few months since your question, but maybe this will still help.
Assuming you're using isapi_rewrite v3, try this:
RewriteCond %{HTTP:Host} ^www\.
RewriteRule ^/forum/(.*\.asp)$ http://archive.mywebsite.co.uk/forum/$1 [NC,R=301]
The first line looks for host beginning with www. (with the trailing dot). This makes sure you don't get an infinite loop, redirecting archive to itself. The trailing dot is being picky at doing only www, and not others like www2.
The second line looks for /forum/, then captures (...) any characters .* and literal dot and asp \.asp, ending the url $
It then goes to your subdomain in the /forum/ folder (since /forum/ wasn't captured, we need to repeat it), and the entire rest of the url that was captured $1.
The NC means not case-sensitive, so all this can be mixed upper and lower case.
The R=301 means redirect, and make it a permanent 301 redirect since this isn't temporary.
In the v3 rules, querystring parameters are handled entirely separately from the rules, so you don't have to do anything at all here. If there are no parameters, then this works. If there are parameters, they are passed on as in your question above.
This ignores http vs https. It will redirect to http, so if the original was https, there will probably be a browser warning. There are ways to handle it, but I didn't want to clutter the basic answer.
Having the domain itself in the rewrite rule is always a little weird looking to me, since you might want to move it around. You can capture the rest of the host in the first line, and use it in the second.
RewriteCond %{HTTP:Host} ^www\.(.*)$
RewriteRule ^/forum/(.*\.asp)$ http://archive.%1/forum/$1 [NC,R=301]
This is similar to above, with the addition that the host after the www-dot is captured, to the end of the line (.*)$ I'm not sure the $ is required here, but it makes it explicit we want it all.
RewriteCond captures are numbered with a percent sign, so %1 in the rewrite rule substitutes the host after the subdomain, and $1 still means substituting the captured ...asp url.

RewriteRule in htaccess

Could anyone explain the following line please?
RewriteRule ^(.*)$ /index.php/$1 [L]
The parts of the rewrite rule break down as follows:
RewriteRule
Indicates this line will be a rewrite rule, as opposed to a rewrite condition or one of the other rewrite engine directives
^(.*)$
Matches all characters (.*) from the beggining ^ to the end $ of the request
/index.php/$1
The request will be re-written with the data matched by (.*) in the previous example being substituted for $1.
[L]
This tells mod_rewrite that if the pattern in step 2 matches, apply this rule as the "Last" rule, and don't apply anymore.
The mod_rewrite documentation is really comprehensive, but admittedly a lot to wade through to decode such a simple example.
The net effect is that all requests will be routed through index.php, a pattern seen in many model-view-controller implementations for PHP. index.php can examine the requested URL segments (and potentially whether the request was made via GET or POST) and use this information to dynamically invoke a certain script, without the location of that script having to match the directory structure implied by the request URI.
For example, /users/john/files/index might invoke the function index('john') in a file called user_files.php stored in a scripts directory. Without mod_rewrite, the more traditional URL would probably use an arguably less readable query string and invoke the file directly: /user_files.php?action=index&user=john.
That will cause every request to be handled by index.php, which can extract the actual request from $_SERVER['REQUEST_URI']
So, a request for /foo/bar will be rewritten as /index.php/foo/bar
(I'm commenting here because I don't yet have the rep's to comment the answers)
Point #2 in meagar's answer doesn't seem exactly right to me. I might be out on a limb here (I've been searching all over for help with my .htaccess rewrites...), and I'd be glad for any clarification, but this is from the Apache 2.2 documentation on RewriteRule:
What is matched?
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
To me that seems to say that for a URL of
http: // some.host.com/~user/folder/index.php?param=value
the part that will actually be matched is
~user/folder/index.php
So that is not matching "all characters (.*) from the beggining ^ to the end $ of the request", unless "the request" doesn't mean what I thought it does.

Resources