RewriteEngine on
RewriteCond %{HTTP_HOST} ^(^.*)\.mysite.net
RewriteRule (.*) user/index.php?fullname=%1
The above .htaccess file listens for subdomains and forwards requests to a specific file when it finds one.
In the same directory as the RewriteRule there is a directory called 'edit' which I cannot access because of the current .htaccess file. Is there any way I can modify this .htaccess file so that I can enter mysubdomain.mysite.net/edit and not have it redirect me back to user/index.php?
Thanks
Check that the request isn't for a file or directory that actually exists:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(^.*)\.mysite.net
RewriteRule (.+) user/index.php?fullname=%1
RewriteCond %{HTTP_HOST} ^(^.*)\.mysite.net
RewriteRule ^$ user/index.php?fullname=%1
Just add a RewriteCond that exempts edit/ from the rule, like this:
RewriteCond %{HTTP_HOST} ^(.*)\.mysite.net
RewriteCond %{REQUEST_URI} !/edit/?
RewriteRule .* user/index.php?fullname=%1
The second condition means "if the requested URI does not contain /edit/ or /edit as a substring". Customize the regex to your exact needs :). Also, make sure to check out the manual, it has a ton of good info, and nice examples.
Related
I have simple .htaccess with:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/static/).+ [NC]
RewriteCond %{REQUEST_URI} ^(?!/media/).+ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [L]
Everything works properly (page/edit, users/show ...), but when I open browser on URL index/something a will get empty $_GET.
Where can be problem please?
Your current rewrite rule doesn't take the case of /index/something into account. If you were to just use /something then it would be redirected to index.php?query=something. Try adding this rule:
RewriteRule ^index/(.*)$ index.php?query=$1 [L]
EDIT:
Based on the comments we're seeing that Apache is using /index as an alias for /index.php. As a temporary workaround until you figure out the needed changes for the Apache configuration you could probably do:
RewriteRule ^index/(.*)$ index.php?query=index/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [L]
Putting that first rule ahead of the RewriteCond lines will catch the /index case and the rest will be caught by your original rules.
It might be an issue with the Server config as "index" might be the default file. So the routing is being done due to the configuration and not due to the htaccess.
Try checking what file names are treated as valid default names by Apache. Also, what happens if you add /index(.*) as a separate RewriteRule?
I have created a .htaccess file that allows for vanity URLs. However, I am no longer able to type www.website.com without typing in the index file. I.e. I have to type in www.website.com/index.php in order to see the homepage. This is what my .htaccess file looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
Anyone know how to fix this? Thank you all!
The way you defined your rule increased the complexity.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Above rule means if file name is a directory of file process as it is. after that dont process farther.
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
This rule means map any request uri to profile.php?u=
Now when you request / that is www.website.com it checks the first rule and it fails to match. Then it check the second rule and maps it to profile.php?u=.
One way to fix it, would be check *if $_GET['u'] is empty or / in profile.php. If it is then load the index.php.
Another way is to find a proper regular expression for your user names once found use it here.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(USERNAME_REGEX)$ http://www.website.com/profile.php?u=$1 [NC,L]
The best way to handle this is using PHP,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?uri=$1 [L]
Now index.php will get every uri you pass. Now you can process the URI in index.php.
It may be due to your server set up.
Try DirectoryIndex index.php (see http://davidwalsh.name/directory-index-homepage-htaccess )
Edit (due to me not reading the question properly in the first place)
Have you tried it without the RewriteRule .* - [L] line?
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern
How to redirect example.com/anything to script example.com/seoparser.php?anything using .htaccess
This does not work:
RewriteEngine on
RewriteRule (.*) http://example.com/seoparser.php?$1
because of multiple redirection which ends on address http://example.com/seoparser.php?seoparser.php
You have to test if it is not already rewritten:
RewriteCond %{REQUEST_URI} !^seoparser\.php$ [NC]
RewriteRule ^/?(.*)$ /seoparser.php?$1 [L]
You can check if requested resource is not a file/directory and only then rewrite. For example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /seoparser.php?$1 [QSA,L]
Depending on circumstances (what rewrite logic you have in place/needs to be implemented) this can be much better solution than mentioned by #inti. You will see such combination of rules used in a lot of real applications (for example, WordPress, Magento) and PHP frameworks (most of the time they will have only internal routing logic).
This is a bit different approach (mostly from organisational point of view):
RewriteEngine On
# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# Your rewrite rules (file/folder does not exist, we may need to route it elsewhere)
RewriteRule (.*) /seoparser.php?$1 [QSA,L]
I'm trying to use a sub-directory as the root folder for one of my domains. Using .htaccess, I use mod_rewrite's to get the job done. Here's the code I already have:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]
This gets the job done, but not entirely. For example:
If I goto http://domain.com/, it displays index.php from inside the domain folder. If I goto http://domain.com/test/, it will display the contents (or 403) of the test folder. BUT if I goto http://domain.com/test (for use of shortcuts, or even to display the folder) I get redirected to http://domain.com/domain/test/.
That is not supposed to happen. If anything, it either does a mask from the .htaccess (if test is being used) or should just goto http://domain.com/test/. I have tried to figure out a way around this, and I cannot. So I am seeking your help! :)
Any and all help is greatly appreciated.
Try this: its a little crude, but should do what you want. There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters).
I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. Let me know if it works at all.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]
The commenter is right, if you want to use a sub-directory as the root folder for one of my domains , just configure it in you apache virtual host configuration (DocumentRoot).
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(.*)$ /domain/$1/ [L]
The above code successfully does everything I wanted it to do.
Use RewriteBase like this:
RewriteBase /f2f/f2fweb
What helped me is auto.htaccess generator on my host!!!!
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain.com/TEST//.*$ [NC]
RewriteRule \.(js|css|jpg|gif|png|bmp|mp4|3gp|m4a|m4r|aac|mp3|ogg|wave)$ - [F]