I currently have an .htaccess file to rewrite index.php out of all of the base URL's, example example.com/index.php/home -> example.com/home
The .htacces file is as follows in the root of my public_html folder:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|adminer|resources|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
As far as I know, this rewrites any /URL that isn't images, css, resources, etc.. to point toward index.php.
I am now trying to add to my .htaccess a rule to rewrite example.com/forums -> fourms.example.com.
With the current rules in place, I receive a 500 - Internal Server Error when I try to visit forums.example.com. I have also tried the following to no avail:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|adminer|resources|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^/forums(.*)
RewriteRule ^forums(.*)$ http://forums.example.com$1 [R=301,L]
How do I change these rules to allow the redirection of index.php and rewrite example.com/fourms to fourms.example.com?
And for the second time today, I have found the answer moments after posting a question:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|resources|forums|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^/forums(.*)
RewriteRule ^forums(.*)$ http://forums.example.com$1 [R=301,L]
I had to exclude /forums from the first rewrite, and add in the second rewrite.
Related
I'm aware of a lot of solutions around there, and tried everything. The problem is, when I put a .htaccess in the codeigniter subfolder, it's not read. I thinks it's because of the AllowOverride directive, but I can't change that.
I tried to skip the main subfolder that is not part of wordpress, one I called "sandbox", in the root htaccess (that's part of wordpress), so it doesn't "inherit" any problem from that rewrite (for example, to not have 404 managed by wordpress in the codeigniter project).
The problem is, as I said, that the .htaccess in the /sandbox/ciproject/ folder is not read. So, I tried this in the main .htaccess, and it worked... for a little time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?sandbox/
RewriteRule . /index.php [L]
RewriteBase /sandbox/ciproject/
RewriteCond $1 ^(application|system|private|logs)
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|img|fonts)
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
That allowed me to make it work for a while... but it's not working anymore. Something more I could try? I already changed the config of the codeigniter project so it doesn't use index.php, but nothing. I only get a server error 500 now, and if I delete the second half of the htaccess, I only get 404 as a response for getting rid of the index.php
I don't know if I'm losing some data, but just ask and I'll edit this, so you're not blind at this one.
Based on my understanding, your code-ignitor part doesn't work because, you have two clashing rewrite rules in your htaccess.
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
These are a clashing rule set. Remove one of the lines and then try. It should work.
Use only this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandbox/ciproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
I have created my website using kohana framework and hosted in godaddy. I'm getting 404 page not found error in all links except home page. I have attached .htaccess file here. Kindly provide any ideas regarding this isuse.
# Turn on URL rewriting
RewriteBase /
RewriteEngine On
# Installation directory
#RewriteBase /kohana/
# Protect application and system files from being viewed
RewriteRule ^(application|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
# RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$0 [L]
Make sure that .htaccess is in the same directory as all .php files are. /public_html/ or a folder like this.
Working Example
RewriteEngine On
//Optional: Redirect www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
//Optional: Redirect http:// to https://
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
//Edit the URL (Without parameter)
RewriteRule ^login/?$ login.php [QSA]
RewriteRule ^logout/?$ logout.php [QSA]
//Edit the URL (With parameter)
RewriteRule ^user/edit/([0-9]*)/?$ user_edit.php?user_id=$1 [QSA]
If you are hosting on GoDaddy you usually don't need to make a .htaccess for the index.php file.
It happend to me once, i donĀ“t know if it will be the same problem for you but i know that some hosting servers work using case sensitive urls
Example:
if your file has this name= "Login.php"
and if you try to acces to it like this= "login.php" it will not work and that is also applied to the name of folders.
It looks like you got a question mark in your last line that doesn't belong there, it should be:
RewriteRule ^(.*)$ index.php/$0 [L]
Or else try:
RewriteRule .* index.php/$0 [PT]
I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.
Directories to exclude: "admin" and "user".
So http requests: http://www.domain.com/admin/ should not be passed to index.php file.
ErrorDocument 404 /index.php?mod=error404
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
Try this rule before your other rules:
RewriteRule ^(admin|user)($|/) - [L]
This will end the rewriting process.
What you could also do is put a .htaccess file containing
RewriteEngine Off
In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.
add a condition to check for the admin directory, something like:
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
If you want to remove a particular directory from the rule (meaning, you want to remove the directory foo) ,you can use :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/foo/$
RewriteRule !index\.php$ /index.php [L]
The rewriteRule above will rewrite all requestes to /index.php excluding requests for /foo/ .
To exclude all existent directries, you will need to use the following condition above your rule :
RewriteCond %{REQUEST_FILENAME} !-d
the following rule
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php$ /index.php [L]
rewrites everything (except directries) to /index.php .
We used the following mod_rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects (permanently with a 301 redirect) all traffic to the site to http://www.newdomain.com, except requests to resources in the /test and /my-folder directories. We transfer the user to the exact resource they requested by using the (.*) capture group and then including $1 in the new URL. Mind the spaces.
RewriteEngine On
RewriteRule ^(wordpress)($|/) - [L]
I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.
Directories to exclude: "admin" and "user".
So http requests: http://www.domain.com/admin/ should not be passed to index.php file.
ErrorDocument 404 /index.php?mod=error404
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
Try this rule before your other rules:
RewriteRule ^(admin|user)($|/) - [L]
This will end the rewriting process.
What you could also do is put a .htaccess file containing
RewriteEngine Off
In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.
add a condition to check for the admin directory, something like:
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
If you want to remove a particular directory from the rule (meaning, you want to remove the directory foo) ,you can use :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/foo/$
RewriteRule !index\.php$ /index.php [L]
The rewriteRule above will rewrite all requestes to /index.php excluding requests for /foo/ .
To exclude all existent directries, you will need to use the following condition above your rule :
RewriteCond %{REQUEST_FILENAME} !-d
the following rule
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php$ /index.php [L]
rewrites everything (except directries) to /index.php .
We used the following mod_rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects (permanently with a 301 redirect) all traffic to the site to http://www.newdomain.com, except requests to resources in the /test and /my-folder directories. We transfer the user to the exact resource they requested by using the (.*) capture group and then including $1 in the new URL. Mind the spaces.
RewriteEngine On
RewriteRule ^(wordpress)($|/) - [L]
I have one .htaccess file in the public_html folder of my server that lets me keep my primary domain in a subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mrmikeanderson.com$
RewriteCond %{REQUEST_URI} !^/mrmikeanderson/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mrmikeanderson/$1
RewriteCond %{HTTP_HOST} ^(www.)?mrmikeanderson.com$
RewriteRule ^(/)?$ mrmikeanderson/index.php [L]
In that subfolder is another .htaccess with more rewrites to turn urls ending with things like /index.php?page=about into just /about:
RewriteEngine On
RewriteRule ^$ index.php?page=home
RewriteRule portfolio index.php?page=portfolio
RewriteRule resume index.php?page=resume
RewriteRule about index.php?page=about
RewriteRule contact index.php?page=contact
The last four pages work, but my rewrite for just the domain name (\^$) is broken. Everything works on my local MAMP server, but the first .htaccess file is not present there, so I'm thinking that the two are conflicting. Any web dev champs able to see what's going wrong?
I'm assuming you have a /mrmikeanderson/ folder where the 2nd htaccess file is. The reason why the
RewriteRule ^$ index.php?page=home isn't being applied is because you are redirecting the / request to mrmikeanderson/index.php. So either change this rule:
RewriteRule ^(/)?$ mrmikeanderson/index.php [L]
to
RewriteRule ^(/)?$ mrmikeanderson/index.php?page=home [L]
or change this rule in the other htaccess file:
RewriteRule ^$ index.php?page=home
to
RewriteRule ^(index.php)$ index.php?page=home
Or you can change your index.php file to assume the variable page is home by default.
Try commenting out:
RewriteRule ^(.*)$ /mrmikeanderson/$1
It looks like the regex ^(.*)$ will match anything including blank strings, which would conflict with RewriteRule ^$ index.php?page=home
Edit:
Try using ([A-Za-z0-9]+) in place of the ^(.*)$ which should give you:
RewriteRule ^([A-Za-z0-9]+)$ /mrmikeanderson/$1
You can always set up a rewrite log to see what's going on http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog