can anybody please tell me the rewrite rule so I can make a URL like this:
http://www.mysite.com/index.php?page=directory1/page
what I'm trying to do is remove the "index.php?page=" so i get a
"http://www.mysite.com/directory1/page"
This is how Drupal does it; it's very similar to toneplex's example, but it doesn't mess around with extension checking and it adds an extra check on favicon.ico, which many browsers automatically request; this saves an extra hit on your PHP code if you're missing favicon.ico.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I'm assuming you are trying to bootstrap everything via the index.php file. So try this out. Any file or directory that doesn't exist will be force through the index.php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
Related
I am trying to work with pretty url's. but it will go to 404.php
I don't know what is wrong with my .htaccess . I'm new with this prettyurl
.htaccess
RewriteEngine on
RewriteRule ^([0-9]+)/$ test.php?id=$1
# not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]
in my test.php
here is the only code inside in my test.php
if(isset($_GET['id'])){
$id = $_GET['id'];
echo $id;
}
but it's not printing.
I point my browser like this
http://localhost/myprojectfolder/test/1
structure of my project
myporjectfolder
- index.php
- .htaccess
- test.php
Thank you in advance.
I think you have a couple of issues. You are pointing to http://localhost/myprojectfolder/test/1 but you don't have /test in your rule. You have it only accepting digits only. Also you are not using a slash at the end of the URL but your rule says it should be there so that won't match either, unless you make it optional.
Also you are directing non existent files to 404.php. Well making pretty URL's which you are trying to do are non existent files as well. So that could cause problems.
Anyway, Try your rules this way for a URL like this
http://localhost/myprojectfolder/test/1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]
Or remove the /test folder which seems unnecessary anyway
http://localhost/myprojectfolder/1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]
I am using cakephp and now I need to do a inner redirect to an .html if the file exists inside a cache folder.
So, lets say there is a controller CTRL and action ACT, the url for it is:
domain.com/ctrl/act
Now, I want my htaccess to check if there is an .html inside teh cache folder with the same name of the controller and the action, like:
/cache/ctrl/act.html
if the file exists, just send it and don't touch the php.
I got it to work but now the cakephp is not being processed, the server returns a 500 error.
Can you please help me with this. I also need to check if there is an /index.html within the cachefolder/ctrl/ because the links sometimes has only the controller name.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}.html !^/cache
RewriteRule ^(.*)$ cache/$1.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Change your first rule to this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app/webroot/cache/$1.html -f
RewriteRule ^(.+?)/?$ cache/$1.html [L]
Add following in routes.php
Router::parseExtensions('html');
This will strip off the .html so cakePHP can just work 'normal'
Couldn't use the htaccess.
I had to use the beforefilter within AppController.
thanks
I'm running in to a mod_rewrite issue where the second rule in my .htaccess file is overriding the first. The .htaccess file in question looks like the one below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /api/v1/(.*)$ api/v1/index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
The issue that I'm seeing is this:
If I go directly to http://example.com/path/appname/api/v1/valid/endpoint the first RewriteRule triggers correctly and I get the result back from the API.
However, say I visit http://example.com/path/appname/app - a page which has been rewritten according to the second RewriteRule. This page makes AJAX requests to the api/v1 page. Those requests are instead directed through the second RewriteRule and send to my base index.php page.
I'm confused on how this could be, as my understanding is that the [L] flag prevents any further rules from being run once it matches and thus once any request that has 'api/v1' in it should catch that and stop checking for any further matches. What do I need to change in order for this to work correctly?
Thanks!
You should exclude the segment path for the previous rule-set, so it is not processed again. Like this:
# Don't redirect/map when folders or files exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Exclude the previous path
RewriteCond %{REQUEST_URI} !api/v1/? [NC]
# Prevent loops
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule . index.php [L]
</IfModule>
Replace the last 4 lines in you question with the above lines.
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?
My end goal is to have something like this.
127.0.0.1/site/search/search-term
Rather than,
127.0.0.1/site/search.php?term=
I have tried two pieces of code, first being.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ../search.php?term=$1
And i put this inside a folder called /search. The HTACCESS works perfectly but the css gets messed up for my website, it just shows everything as plain text without any styling.
Secondly i tried
RewriteEngine On
RewriteBase /search/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ../search.php?term=$1
And put that in 127.0.0.1/site/ root. But when i tried 127.0.0.1/site/search/searchterm I get a 404.
Can anyone see where i am going wrong? I am guessing the first option is the best one to work towards getting fixed as it is so close, i look through the source of the page i receive after using the vainty url and i think it might be something to do with how im linking the style sheet? It works if i put the full address in for the link (127.0.0.1/site/main.css) But then all it fails on all my jquery includes etc. Is it wise to just put the full address in when ever i want to use vanity urls?
First make sure you're using absolute paths in CSS, JS and images files. That means path to these resources should either start with a slash / or http://. If you don't already have it then please make necessary code changes. Once that is done use following .htaccess in $DOCUMENT_ROOT (not in $DOCUMENT_ROOT/site):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^site/search/(.+)/?$ site/search.php?term=$1 [L,QSA,NC]
EDIT: As per your comments here is the code to make a URI in lowercase:
First add this line in section OR at the end of your httpd.conf file:
RewriteMap lc int:tolower
Then have your rules like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^site/search/(.+)/?$ site/search.php?term=${lc:$1] [L,QSA,NC]