Rewrite url in htaccess to correct - .htaccess

I have link like this: http://site.crm.int/project_discussions/test
I need redirect it to : http://site.crm.int/project_discussions/index.html?url=test
Text 'test' is changable. Can be for example 'december', 'meetting01' etc.
I tried it in my .htaccess:
RewriteRule ^\/project_discussions/(.*)$ \/project_discussions\/index.html?url=%1 [L]
But have 404 error.
Also tried this:
RewriteCond %{REQUEST_URI} ^/project_discussions/(.*) [NC, L]
RewriteRule ^(.*)$ /index\.html?url=$1 [L,NC]
And have 'Internal Server Error'
How can i fix it? thanks

With your shown samples, please try following .htaccess rules file. Make sure .htaccess file and folder project_discussions are residing in same root folder. Also your index.php OR index.html file should be inside your project_discussions folder.
Make sure to clear your browser cache before testing your URLs.
Also I have fixed your previous rule for handling internal rewrites.
RewriteEngine On
RewriteBase /project_discussions/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ $1/index.html?url=$2 [R=301,NE,QSA,L]

Related

.htaccess redirect non-clean URL with two variables to clean URL

I am trying to redirect www.bostonmabl.com/400hitter/boxscore/?SeID=332&GmID=93091 to www.bostonmabl.com/400hitter/boxscore/332/93091/ in the event that a user enters an old non-clean URL. Below is my .htacess file which I tried placing in both the 'root' directory and the '400hitter' directory, yet neither is working. Any help is greatly appreciated.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^SeID=([^&]+)&GmID=([^&]+) [NC]
RewriteRule ^/400hitter/boxscore/index\.php$ https://bostonmabl.com/400hitter/boxscore/%1/%2/? [R=301]
With your shown samples, please try following htaccess rules. Make sure your .htaccess rules file and index.php are residing in same folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect to new URL in browser.
RewriteCond %{THE_REQUEST} \s/(400hitter/boxscore)/\?SeID=([^&]*)&GmID=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^400hitter/boxscore/(\d+)/(\d+)/?$ index.php?SeID=$1&GmID=$2 [QSA,NC,L]
NOTE: This solution assumes that you want to rewrite(in backend) to index.php(by seeing your attempted code) and pass SeID and GmID parameters/query string into it.

mod_rewrite a sub directoy to child subdirectory

I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder
With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

Rewriterule for PHP files fails

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

.htaccess rewrite with REST style URL 500 error

I'm trying to implement a REST-style URL with a mod-rewrite turned on in .htaccess. There's a bit of a kicker which is that I'm developing in a test environment (new cpanel account). Here's the .htaccess:
RewriteEngine on
#REMOVE THIS LINE ON SITE LAUNCH!
RewriteBase /~myNewAccount/
#Hide .php extensions for cleaner URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options All -Indexes
The URL I CAN use looks like this:
www.example.com/~myNewAccount/index.php/id/50
I can access the PATH_INFO here, but when I try to do this:
www.example.com/~myNewAccount/index/id/50
...I get a 500 internal server error. I've tried implementing the solution found here by Gumbo but that mucks things up.
Ideas on what might be causing this?
Try this rule:
RewriteRule ^index(/.*)?$ index.php$1 [L]
Or if you don’t want index to be in the URL path at all:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php/$0 [L]

Resources