For my site I have a RewriteRule that points the URL http://www.mysite.com/work to a work.php file. I also have a directory called "work" that has files in it, like project1.php, project2.php, etc...
What rules would I have to write so that the URL http://www.mysite.com/work knows to go to the work.php file, but the URL http://www.mysite.com/work/project1 knows I mean to go inside the directory "work" and display the project1.php file?
EDIT: Should point out, this is what I'm currently working with:
RewriteEngine On
RewriteBase /beta/
RewriteRule ^([a-z]+)$ $1.php [L]
Any additional tips to improve this security-wise? (Stopping directory jumping, etc...)
Try this:
RewriteEngin On
RewriteBase /
RewriteRule ^work$ /work.php [QSA,L]
That will ensure that http://www.mysite.com/work (no trailing slash) will go to your work.php file.
If you also want http://www.mysite.com/work/ (with trailing slash) to go work.php, add this line just above the last RewriteRule.
RewriteRule ^work/$ /work [R=301,QSA,L]
That will redirect it to the URL with no trailing slash thus, displaying the work.php file.
UPDATE: Since you already have a RewriteBase directive, just put the RewriteRule line(s) right after your RewriteBase but before your RewriteRule as the rule you're using a catch-all and will match everything.
This is the answer to your question. It works for me.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)$ $1.php [L]
Simply remove beta/
Try this rule:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php [L]
Related
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
I have a web application in a directory suppose xyz. I am trying to redirect all requests to a sub directory www. My current .htaccess code is:
RewriteEngine On
RewriteRule (.*) /www/$1 [L]
But, when I visit http://example.com/xyz/some_url the request is redirected to http://example.com/www/some_url rather http://example.com/xyz/www/some_url which I want.
Update: Sorry, I forgot to mention the directory xyz as in my case is likely to renamed. So, this directory doesn't need to be hard coded in the rewrite rule
Place this rule in /xyz/.htaccess:
RewriteEngine On
RewriteBase /xyz/
RewriteRule ^((?!www/).*)$ www/$1 [L,NC]
If your DocumentRoot does not contain the xyz path then you'll need to specify in the RewriteRule.
E.g.: RewriteRule (.*) /xyx/www/$1 [L]
This should work or be close I think:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/([^/]+)$ $1/www/$2 [L]
Use a relative path and two .htaccess files
You need only remove the leading / to rewrite to a url relative to the current folder:
# /xyz/.htaccess
RewriteEngine on
RewriteRule (.*) www/$1 [L]
This will rewrite /xyz/foo to /xyz/www/foo.
To prevent redirect loops add a .htaccess file to the www subfolder e.g.:
# /xyz/www/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Adjust as appropriate.
I have edited the httpd.conf file like:
changed to AllowOverride All
Here is my .htaccess file:
RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [R=301]
RewriteRule ^contact.php$ /contact/ [R=301]
RewriteRule ^shirts.php$ /shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /shirts/%1/? [R=301]
And the folder structure is like this:
shirts4mike
+ contact
+ css
+ img
+ inc
+ receipt
+ shirts
|_ shirt.php
|_ shirts.php
- .htaccess
- favicon.ico
- index.php
I've tried many times, but it still showing index file does not exits, when I click the shirts in the home page.
I think xampp does ignore the .htaccess file.
If so, How should I fix?
The problem seems to be that you try to rewrite an url in a subdirectory, but it links to the http-root directory. You need to have the .htaccess file in the subdirectory. You'll notice that a rule like RewriteRule ^test$ /testing-is-fun will match on an url http://example.com/shirts4mike/test, but rewrites to a file testing-is-fun in your http-root. We therefore need to fix the rewriting part of the rule.
The fix is having an relative url, instead of an absolute url. This can be done by removing the prefix /. For redirects, define RewriteBase with the subdirectory in it. I would add the [L] flag to every rule too. Your .htaccess should look something like this:
RewriteEngine On
RewriteBase /shirts4mike/
RewriteRule ^shirts/$ shirts/shirts.php [L]
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1 [L]
RewriteRule ^receipt.php$ receipt/ [R=301,L]
RewriteRule ^contact.php$ contact/ [R=301,L]
RewriteRule ^shirts.php$ shirts/ [R=301,L]
RewriteRule ^(shirts/[0-9]+)$ $1/ [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ shirts/%1/? [R=301,L]
As a side-note: I encourage you to do not use [R=301] while testing rewriterules. Some browsers cache a permanent redirect to increase performance. This is fine if your rules work as you expect them to work, but if that is not the case, further tests might link to your old page. This means when you change your .htaccess, you'll have to clear your cache to see the current situation.
I'm working with mod rewrite, but my code doesn't work. It worked for a time.
I have stripped the code.
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/$ index.php?test=$1
PHP
<?php
var_dump($_GET['test']);
?>
If I go to the index.php it displays NULL.
I don't understand why it doesn't work anymore. I hope you can help me.
P.s. I have tested whether the .htaccess file is loaded by making a login form with .htaccess.
You need to get rid of the first slash:
RewriteRule ^(.*)/$ index.php?test=$1
And even then your rule will only apply when you enter a url that ends with a forward slash, so for example:
/index.php/
If you want it to work with any url, you need to remove the last slash as well:
RewriteRule ^(.*)$ index.php?test=$1
Edit: To avoid rewriting of existing files and directories you need add some conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?test=$1
You need to remove the first slash in your rule
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ index.php?test=$1
I have a .htaccess file which arranges that all requests go through index.php.
Now i would like to make an exception for rss.php. to go straight throuh rss.php.
How do I do this?
This is how it looks like now:
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
Thanks.
Put this before the last line.
RewriteCond %{REQUEST_URI} !^/rss\.php$
You can exclude any existing file with an additional RewriteCond directive:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Or alternatively do a rewrite that matches the file then skips the rest of the rewrite tests.
By putting the following line before your existing RewriteRule will redirect without going through index.php.
RewriteRule ^/rss.php /rss.php [L]
I came across this page while hunting for a way to do this with my /robots.txt file.
mod_rewrite with apache1.3