htaccess rewrite folder [closed] - .htaccess

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a .htaccess that looks like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !static/(.*)\.
RewriteRule ^(.*)$ index.php?controller=$1 [QSA]
It works fine. /static folder request remain intact, while others execute index.php file.
But now I have to add another rule. When a user navigates to /action/something, then /actions/something.php should be executed. But when I add the following line
RewriteRule ^action/(.*)$ actions/$1.php [QSA]
it breaks requests to static folder.

There is no reason, why it should break static, unless you wrote the new rule right after RewriteCond. What you should do however, rewrite to an absolute URL
RewriteEngine On
RewriteCond %{REQUEST_URI} !static/(.*)\.
RewriteRule ^(.*)$ /index.php?controller=$1 [QSA]
RewriteRule ^action/(.*)$ /actions/$1.php
The RewriteCond looks unusual. Unless there is a reason to rewrite static pages without a dot ., you should reduce the RewriteCond to just
RewriteCond %{REQUEST_URI} !static/
Update:
To prevent the infinite rewrites, you must add another exclusion condition
RewriteCond %{REQUEST_URI} !^/index\.php$
and action must be excluded as well
RewriteCond %{REQUEST_URI} !/actions?/
All put together gives
RewriteEngine On
RewriteCond %{REQUEST_URI} !/static/
RewriteCond %{REQUEST_URI} !/actions?/
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(.*)$ /index.php?controller=$1 [QSA]
RewriteRule ^action/(.*)$ /actions/$1.php

Related

Remove part from the Url using htaccess [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I ve looked into many examples in stackoverflow and couldn't find a working solution for me.
I have a wordpress blog set up in a subdirectory.
The blog sits under www.domain.co.uk/wordpress/
In order for my permalinks to work I did an htaccess rewrite rule which is this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.co.uk$
RewriteRule ^(/)?$ http://domain.co.uk/wordpress [L]
Is there a way to take out the /wordpress part on the URL?
I need it to point there but I want the /wordpress to be removed.
Any ideas?
I have now managed to solve this issue following this guide: http://www.optiniche.com/blog/145/wordpress-tutorial-install-wordpress-in-a-different-directory/
Remove the wordpress from the redirect in your rule (the http://domain.co.uk/ means there's an implicit redirect) and add a specific rule to internally rewrite instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$ [NC]
RewriteRule ^ http://domain.co.uk/ [L,R=301]
# now silently rewrite to wordpress
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1 [L]

.htaccess rewriterules exclude directory [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I made all my htaccess rewriterules but now I want to exclude all the directories and files from it. All hints regarding this htaccess are welcome, because I haven't got experience with rewrite rules...
I figured out that the RewriteCond will only work for the first rule that follows.
Thus how can I exclude all files and directories from all rules?
Allow from All
RewriteEngine on
SetEnv DEVELOPMENT_ENVIRONMENT true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#One url parameter
RewriteRule ^([a-zA-Z0-9_]*)/$ index.php?language=$1
RewriteRule ^([a-zA-Z0-9_]*)$ index.php?language=$1
#Two url parameters
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)$ index.php?language=$1&page=$2
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/$ index.php?language=$1&page=$2
#Three url parameters
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)$ index.php?language=$1&page=$2&feed=$3&message=$3&page_number=$3&id=$3
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/$ index.php?language=$1&page=$2&feed=$3&message=$3&page_number=$3&id=$3
#Four url parameters
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_\-]*)$ index.php?language=$1&page=$2&id=$3
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_\-]*)/$ index.php?language=$1&page=$2&id=$3
#Five url parameters
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)$ index.php?language=$1&page=$2&feed=$3&year=$4&month=$5
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/$ index.php?language=$1&page=$2&feed=$3&year=$4&month=$5
Thus how can I exclude all files and directories from all rules?
I guess you mean:
Thus how can I exclude all files and directories THAT EXIST from all rules?
Like this:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Replace the first 2 RewriteCond lines with the above set.

.htaccess Add an exception to 404 redirect to particular sub-directory [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
The sub-directory is /addon/ it stores all data for my sub-domains a.k.a addon domains.
In this sub-directory I also store my primary domains data, my primary domain is website.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?website.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
Okay so that part is done, if I enter website.com/addon/mysecondwebsite-com I get a 404.
If I enter mysecondwebsite.com I see the contents of website.com/addon/mysecondwebsite-com.
This is great, thats what I wanted.
Now the tricky part, I already set up website.com to use website.com/addon/website-com. No need to tell me how to configure /addon/website-com for website.com. I need to disable 404 for website.com which is configured to use /addon/website-com/ but I also need to make sure if I enter website.com/addon/website-com that this directory is returned a 404.*
My rewrite conditions mention if I enter /addon/* on my primary domain (website.com) I will be returned a 404 error. This is great for my addon domains because they use /addon/ and not my primary domain. But how do I also block /addon/* but still use my primary domain?
website.com = 404 / BAD (website.com/addon/website-com)
mysecondwebsite.com = 200 / Good (website.com/addon/mysecondwebsite-com)
website.com/addon/mysecondwebsite-com = 404 / Good
website.com/addon/website-com = 404 / Good
Just so you get the full picture this is my .htaccess file for /public_html/
# Part one block direct access to /addon/ folder
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?website.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
# Part two using /addon/website-com/ as website.com directory
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteCond %{REQUEST_URI} !^/addon/website-com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /addon/website-com/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteRule ^(/)?$ addon/website-com/index.php [L]
Replace your 404 handler with this:
ErrorDocument 404 /404.shtml
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+addon/ [NC]
RewriteRule (?!^404\.shtml$)^.*$ - [L,R=404,NC]

subdomain redirection in htaccess [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I was wondering if the following scenario is possible using htaccess rules.
I want one subdomain to be redirected to another url.
I have contact the server admin to add the "test" subdomain to "example.com" domain.
The main domain has no other subdomains.
What rule must i put in htaccess to do achieve:
http://test.example.com to be redirected to http://www.something-else.com.
NOTE: www.something-else.com is a complicated url (200 chars length)
EDIT
My full htaccess file now looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/redir.php [L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The htaccess file is located to example.com root directory.
I have no physical access to test subdirectory, although i know it exists - ping to that url shows me the IP of example.com
Typing test.example.com to the browser's address bar has no effect. White screen, no redirection, no nothing. If some page exists there, i know not.
For static urls:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ https://www.somdomain.com/a/folder1/somepage?var=xxx&var2=xxx&var3=xxx&var4=http://another-domain.com/folder1/xxxxx/?&var5=xxx&var6=xxxx [R=301,NC,L]
For Dynamic urls (if the original domain has folders that needs to be moved to the other domain):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ https://www.somdomain.com%{REQUEST_URI} [R=301,NC,L,QSA]
From your edit:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ redir.php [R=301,NC,L]

How to remove .php extension and add slash on the url? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Here is my current .htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
it is works only in removing .php extension
from
http://localhost/mysite/news.php?category=cat1&id=1
to
http://localhost/mysite/news/cat1/1/
and from
http://localhost/mysite/news.php?category=cat1&year=2011&month=10&day=25&id=1
to
http://localhost/mysite/news/2011/10/25/1
How to write complete .htaccess for the clean url above?
Try This
RewriteRule ^(.*)\/$ $1.php [NC]
for example
http://www.exapmle.com/contact-us/
Add an optional / to your pattern:
RewriteEngine On
# rewrite news articles and pass news id as a GET parameter to news.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/news/(\d+)/?$ /mysite/news.php?newsid=$1 [NC,L,QSA]
# rewrite all other page requests to .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/(.*)/?$ /mysite/$1.php [NC,L,QSA]
You could just try a simple RewriteRule:
RewriteEngine on
RewriteRule ^/?(\w+)/?$ $1.php
This tacks on .php to any file-looking url: example.com/somethin becomes example.com/somethin.php, example.com/something/else/ becomes example.com/somethin/else.php, etc.
The only problem with this is if you try to access an actual folder, like example.com/images or something.

Resources