I'm trying to set up a site that forwards everything but the root directory and index into a variable. I have the htaccess file set up like this right now:
Options +FollowSymlinks
RewriteEngine on
RewriteRule -(.*)$ http://blah.com/blah.php?name=$1 [R,NC]
just so that the index works and anything that starts with a hyphen(-) is rewritten
I would like to be able to have anything that isn't the index file rewritten, and still allow the index file be accessed via blah.com and blah.com/
Any ideas?
Try this :
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/blah.php
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*) http://blah.com/blah.php?name=$1 [R,NC]
If by any chance you still haven't figured this out, this should work:
RewriteCond %{REQUEST_URI} !^(/|/index.php|/blah.php)$
RewriteRule ^(.*)$ blah.php?name=$1 [R]
Related
On a site I'm working on, if you enter the url, plus 1 directory, the htaccess adds a trailing slash.
So, this: http://www.mysite.com/shirts
Becomes this: http://www.mysite.com/shirts/
The htaccess that runs the site is quite long and complex, so it's not easy to find or test which rule is causing the rewrite. I was able to track down the issue to this line of code (I think):
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Does this rule match the behavior I'm describing above? It seems to be the cause, but it doesn't make logical sense to me. I don't unsderstand where the trailing slash is coming from.
Can someone shed some light on this for me? Thanks in advance.
Edit: MORE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
By default apache will add the ending /, you will have to use:
DirectorySlash Off
To disable that behavior which is caused by mod_dir, you can read more about it here.
However if you're trying to remove the / to fix images not showing. That is not the right way to do it, you should instead use the HTML base tag, for example:
<BASE href="http://www.yourdomain.com/">
Read more here about it.
Your current rule as you have updated on your question:
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Means:
if domain on the URL is only mysite.com
redirect current URL to domain with www.
So an example of it would be, if you access:
http://domain.com/blog/some_blog_article
It will redirect the user to:
http://www.domain.com/blog/some_blog_article
Note how it retains everything and only add the www. to the domain.
If you really want to redirect it regardless here is one way to do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# check if it is a directory
RewriteCond %{REQUEST_FILENAME} -d
# check if the ending `/` is missing and redirect with slash
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# if file or directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# and we still want to append the `/` at the end
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
when it come to ht-access, i am not very good at this. i tried reading but cant understand it well
i have my website: http://www.website.com/folder1/folder2/param
and i need to rewrite it into: http://www.website.com/folder2/param
can anybody help me with this and tell me what each line is actually doing.
i need to rewrite only if folder2 is the next folder and not other folders
so:
/folder1/folderxxx/param
will remain as it is.
i did this:
RewriteCond %{REQUEST_URI} !([a-z]+)/folder1.php
RewriteCond %{REQUEST_URI} ^/folder1.php$
RewriteRule ^(.*)$ folder/folder1/ [L]
and when i go to www.website.com/folder.php i get the page but i don't need the .php and if i remove it it doesnt work
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^folder2/(.*)$ /folder1/folder2/$1 [L]
And you may need this to remove it from the URL in the browser's locaiton bar:
RewriteCond %{THE_REQUEST} \ /folder1/folder2
RewriteRule ^folder1/folder2/(.*)$ /folder2/$1 [L,R=301]
I have a local Apache server set up on my machine, with wildcard DNS in place. I have it set up so that it works like [foldername].loc. So, for instance, a folder under my htdocs folder called MyDomain, would be accessed via mydomain.loc. This code works fine, and the code in my .htaccess in my htdocs is below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]*\.)?([a-z0-9-]+)\.loc$ [NC]
RewriteCond %3::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule (.*) /%3/$1 [PT,QSA]
Now, the above code also passes through subdomains, such as "john.mydomain.loc". Now, I have the following folder structure in the folder MyDomain:
MyDomain
- active
- index.php
- working
- index.php
.htaccess
In the .htaccess of MyDomain is the following code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteRule ^(.*)$ /active/$1 [L]
What this should do, if I understand correctly, is take http://live.mydomain.loc/ and rewrite it to be http://mydomain.loc/active/. Note that I said rewrite, not redirect.
With the code above, however, I get a message in the Apache error log:
[client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If I change the .htaccess of MyDomain to read as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /active/$1 [L]
When I use this code, it always comes up with a 403 error, saying I don't have permission to view the folder /mydomain/. If I set Options +Indexes, I only see the folder index of /mydomain. So where along the line is the above code failing?
I have also tried the above code with RewriteCond %{REQUEST_URI} !^/active/. This has made no difference in the results.
I have tried this for over two days, and I can't figure it out. I hope the brilliant minds of StackOverflow can help figure this out. :)
Try setting the rewrite base to where the file actually is:
RewriteEngine On
RewriteBase /MyDomain/
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ active/$1 [L]
And making the /active/ relative: active/
I solved this using the code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/active/
RewriteRule ^(.*)$ active/$1 [L]
The primary fix was RewriteCond %{REQUEST_URI} !^/mydomain/active/. The difference is, the first line I tried, RewriteCond %{REQUEST_URI} !^/active, would match /access/index.php, but not /mydomain/active/index.php.
I hope this helps someone else.
I want to have a multi language site. Now, I have 2 domains. The first one is the main domain. That is website.nl. And i have a domain alias, website.org. So the 2 domains share the same public_html folder.
What I want is that:
website.nl will use the file /index.php/$1 and
website.org will use the file /gb/index.php/$1 (So when the url is website.org/test you will use the file /gb/index.php/test (No url redirect)
I found on another topic on stackoverflow the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} website.org
RewriteRule ^(.*)$ /gb/index.php [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
But this htaccess file won't work. I will get a 500 error. That's all.
Can someone see what's going wrong?
Your rules are looping, otherwise the 2 rules will mess with each other and loop indefinitely (e.g. requesting /foo will result in /index.php/index.php/index.php/index.php... etc thus returning 500). You need to add some conditions to stop the looping. Try changing the conditions and rules to:
RewriteCond %{HTTP_HOST} website.org
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteRule ^(.*)$ /gb/index.php/$1 [L]
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
There's tons of resources online about using .htaccess to rewrite your subdomains and if need be, also rewrite your main domain to a subfolder. I have found plenty answers and most of them are exactly the same. I have been tediously testing these methods and I have the same problem in all cases.
Consider the wanted result:
maindomain.com : rewrite to /public_html/mainsite/
sub.maindomain.com : rewrite to /public_html/sub/
The fastest/cleanest way i have considered is the following:
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ /sub/$1 [L]
This works well except for 1 annoying issue; The line
RewriteCond %{REQUEST_URI} !^/mainsite
Basically prevents a rewrite loop, but if you browse to maindomain.com/mainsite/ it rewrites to /public_html/mainsite/ instead of /public_html/mainsite/mainsite/ hoping to raise a 404 not found. If i remove that line, i get a 500 server error as it goes into a loop :S
The issue is, that any one of these domains needs freedom of creating folders etc. and would like to ensure that there is absolute freedom in the sub-sub folders people create :S
Please could someone help here?
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*)$ /sub/$1 [L]
You need to make it so that it does not match any file within the /mainsite directory not just the root (/mainsite). I would think you would need to do the same to the sub domain also.
actually try this if you are still looking for an answer
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^/mainsite/(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^/sub/(.*)$ /sub/$1 [L]`