htaccess rewrite for changed forum subdirectory - apache

I've searched and tried many of the examples listed on this site, but none seems to work right for me.
We have a running forum in /forum directory, which was previously named /vanilla2. Moving the forum (today) went ok and all internal links work fine. Redirecting the root of previous forum to the new one works ok using this:
RedirectMatch 301 /vanilla2/.* http://www.example.com/forum/
However the problem is because there are many posted links throughout the forum which still point to the old directory and its subdirectories, for example:
http://www.example.com/vanilla2/discussion/54684/blah-blah, which should now be:
http://www.example.com/forum/discussion/54684/blah-blah
Here's my directory structure:
/root
.htaccess (for WordPress)
/forum
.htaccess (for forum)
/wp
/other_dirs and its subdirectories
My knowledge is obviously quite limited so I need to know what exactly to put in which of existing two htaccess files?
Thank you.

Put this code as first rule in your DOCUMENT_ROOT/.htaccess file ( a level above /forum/):
RewriteEngine On
RewriteRule ^vanilla2(/.*)?$ /forum$1 [L,NC,R=301]

Related

Friendly URLs via htaccess

I read lots of tutorials and articles about Friendly URLs through htaccess file, but probably I am missing something.. Thus, I have some questions in order to understand..if someone can help me to clear these out, please!
First of all, before adding any rule to htaccess file,
1. Should I have links like article.php?article_id=1 or the friendly urls I want inside into my files, like: 2016/05/article_name ???
2. I found I link here through another post, that gives a web tool in order to give the rule of the htaccess file: http://www.generateit.net/mod-rewrite/index.php
So, I placed my actual 'ugly' link and gave me right rule to write in htaccess file.
Now, I have uploaded the htaccess file and I am navigating to my website.. If I have the 'ugly' links I can see everything correctly! While I am pressing the friendly url manually there was to issues:
First, I could not see the website's css and javascripts files.. I put the absolute path according to an article, so I guess I am fine with that (is that correct?).
Second, If I press a link let's say the logo, in order to go back to home page, it keeps the rule of htaccess and navigate me to "not found page".. cause the link is something like that: mywebsite.gr/2016/05/index.php, but year and month does not actually exists ...
I would like to have a rule in order to keep the same articles links (I do not have problems about the other links, categories, menus and so on.. just the links of articles). The link I have now is (after all I see and understood):
article.php?article_id=1&year=2016&month=05&name=test-1
and want to have:
mysite.gr/2016/05/test-1.html
*I guess I should mention that the Domain NS does not change yet, and I am working through the temporary link that host provides me.. something like: linux.41.24.23.4.server.gr ... and so on
thanks a lot in advance!! (and sorry for the possible duplicate..)
I think I found a solution... not sure if it is the best one..
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/([a-z0-9\-]+)\/?.html$ category.php?file=$1&year=$2&month=$3&art_name=$4 [NC,L]
</IfModule>
in order to show my links this way:
mydomain.com.gr/2016/06/this-is-an-article.html
of course inside my website, I slit the article name coming from my DB, in order to add the middle slashes and replace any other symbol with 'nothing':
$art_name = "this is an article!";
$art_name = str_replace('!','',$art_name);
$art_name = str_replace(',','',$art_name);
$art_name = str_replace(' ','-',$art_name);
The final link inside my website is:
click me
Furthermore, about the other links: CSS - Js - a tags and so on, as long as I understood there are some options in order to work properly:
You could use Absolute path, or start your links like this (with forward slash in the beginning):
<link href="**/**main.css" rel="stylesheet" type="text/css" />
it's working like an absolute path I believe, cause the file is seeing the root folder (for example for me is localhost - I am working locally with xampp)
I believe I helped someone who have the same questions..

Apache - mod_rewrite how to prefer files instead of directories (if both have same name)?

I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.

How can I redirect people accessing my files as directories?

I have the following situation:
On my webserver I have an instance of websvn running, where specific repositories and revisions can be accessed by a URL like
http://www.myhost.com/listing.php?repname=repository1&path=%2Ftrunk%2Fbackend
Somehow, out there in the wild, a wrong URL is being used to access this
http://www.myhost.com/listing.php/?repname=repository1&path=%2Ftrunk%2Fbackend
(Notice the slash after listing.php)
Now, although the URL works and websvn still shows the webpage, images and stylesheets do not get loaded correctly, since they are referenced relative.
I tried to add an .htaccess file to the webroot to redirect people accessing the file as directory to the correct URL.
I have tried multiple variations and ended up with this file:
RewriteEngine on
RewriteRule ^/listing.php/ listing.php [R=301,QSA]
But, since I am writing here, you already guessed it: It doesn't work.
I also tried
RewriteEngine on
RewriteRule ^/listing.php(.*) listing.php$1 [R=301,QSA]
What am I doing wrong?
Perhaps among other things, a RewriteRule within .htaccess that starts with “^/” will never match anything at all. (Examples that include a leading slash are for the global configuration file.) Remove the leading forward slash and see if that helps.
Also, I recommend changing the 301 to a 307 until you get it working. Otherwise, your browser will cache the 301 result, redirecting on subsequent references without consulting your server at all and likely giving you very confusing results.

htaccess rewrite question

I want to rewrite the url http://mydomain/myapp/fakefolder to http://mydomain/myapp/index.php
I tried the following rule but thats not working
RewriteEngine On
RewriteRule ^fakefolder$ index.php
The .htaccess file is located inside myapp.
Kindly help
Thanks
vineet
To begin with, your folder is not called vineetstore: it's called fakefolder.
The corrected rule works for me so I'd dare say your Apache installation is not configured to read .htaccess files in such location. You can easily test that: make a syntax error on purpose and see whether your site crashes.
Find your virtual host or site definition and make sure you have this directive:
AllowOverride All

.htaccess mod_rewrite rule not working in Ubuntu

My apologies if this is an easy one. I have Googled it up the hizzy to no avail.
I am running Ubuntu 9.04, Jaunty Jackelope and Apache2. After much trouble, I finally enabled mod_rewrite, and my .htaccess file is attempting to do it's thing, but is failing. This is my setup.
In /etc/apache2/conf.d/ I have a file called apeace-public-html.conf. It reads as follows:
# makes /home/apeace/public_html/ available through /localhost/apeace
Alias /apeace /home/apeace/public_html/
And in /home/apeace/public_html/ I have the following .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^test\.html$ index.html
Also in /home/apeace/public_html/ I have a file named index.html, but I do NOT have a file named test.html. It seems to me that this rewrite should show index.html when I try to access http://localhost/apeace/test.html. However, this is the error I get:
Not Found
The requested URL /home/apeace/public_html/index.html was not found on this server.
So the question is, what in the world am I doing wrong?
Much thanks.
-apeace
Just a guess here, but can you try to make the RewriteRule like ^test.html$ /apeace/index.html
From the error message, it seems it is translating `http://localhost/apeace/test.html to http://localhost/home/apeace/public_html/index.html
Your rewrite rule is working correctly since it's telling you it can't find "index.html". If you went to test.html and it said it can't find "test.html" then your rewrite rule would be at fault.
So what this means is that something else is wrong in your setup, whether it's a bad file or directory name somewhere, or whatever else. Make sure there's nothing basic you're overlooking.
But in answer to your question (especially the title), your htaccess is fine.