htaccess redirect for old site url - .htaccess

We have a client, where were building a new site. There old system had all articles under:
http://www.domain.com/articles
The new site is
http://www.domain.com/resources/articles
Within the articles section url can be as long as:
http://www.domain.com/resources/articles/category-name/title-of-article
and there is
http://www.domain.com/resources/articles/page/# for the pagination
The new site will have the same url structure of the old articles except for the /resources part.
I need to have all incoming traffic that comes under:
http://www.domain.com/articles redirected to http://www.domain.com/resources/articles however I need to make sure that the category name and the title of the article in the url string are also placed in from the old string that was requested to the new one.
How can I do this using htaccess?
--- EDIT ---
Here is my current htaccess:
RewriteEngine on
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ ?cat=$1&title=$2&subtitle=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ ?cat=$1&title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) ?cat=$1

On your old site:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
RewriteBase /
RewriteRule ^(articles/.*)$ /resources/$1 [L,R=301,NC]

Related

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

Can't upload files unless I comment out htaccess entries

I have a site that is built on concrete5 (not by me). Now, I added a few lines to the htaccess to add the trailing slash after my urls as the site was loading over both versions.
However, now I cannot upload files to the site. I am just getting errors. There is also an option for users to upload a photo and they get an error too.
The only work around I have is that I can go into htaccess and comment out the lines I added, save it and then upload and the remove the #'s. Whilst this lets me upload in the short term, it's not a viable solution as regular users cannot do this.
My .htaccess is the following:
# -- concrete5 urls start --
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# -- concrete5 urls end --
I comment out these lines for it to let me upload:
# -- concrete5 urls start --
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !index.php
#RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# -- concrete5 urls end --
Whilst it looks like that i can upload but lose the trailing slashes so the urls are all messed up.
Anyone know what is going on? FYI I am not a developer but can understand to a degree.
Have you updated all your internal links (and form submission URLs) to include the trailing slash?
My guess is that you are submitting the form to a URL that does not include the trailing slash. The 301 redirect that follows will lose any submitted POST data (as the request is converted to GET).
You need to make sure that you are submitting your form to the canonical URL.

Page URL named css or js doesn't work

In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.
EDIT
Here's my complete htaccess file:
# my-site.com
Options +FollowSymlinks -MultiViews
#RewriteBase /
#Enable mod rewrite
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]
Since /js and /css are real directories your rewrite rules don't execute because you have this condition:
RewriteCond %{REQUEST_FILENAME} !-d
which means don't run for directories.
You can tweak your .htaccess to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]

Remove site to another domain

I try remove site to another domain. On the new domain site is located in a subfolder, and i have problem with htacces
htacces on my old domain mydomain.com
RewriteEngine on
RewriteCond $1 !^(index\.php|public|media|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
New site location is mydomain.com/site/
Links to pages and images are not good.
If it's in a subfolder you need to add the rewritebase. Make sure this .htaccess is also in /site/.htaccess
RewriteEngine on
RewriteBase /site/
RewriteCond $1 !^(index\.php|public|media|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

codeigniter htaccess with multiple applications

I am using codeigniter. I have front end application and backend application like
/system/
/application/
/front/
/admin/
index.php
admin.php
.htaccess
I want my url like http://example.com/news/article1 (for site)
http://example.com/news/admin (for admin)
In .htaccess I have written
RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin$ admin\.php [L,QSA]
# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin\/(.*)$ admin\.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
the front end is working fine but when I enter mydomain.com/admin it is throwing 404 not found error. Please help me.
Thanks and regards
I'm not that good with .htaccess, but from the CI website, you can use this for your .htaccess rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
and then use routes to rewrite your urls. Much easier than trying to do it in .htaccess files

Resources