How do I get mod_rewrite working on Heroku? - php

I'm trying to rewrite a URL using the following rule:
RewriteRule ^([^/]*)$ /curation.php?id=$1 [L]
and this is an .htaccess file I created:
RewriteEngine On
RewriteRule ^([^/]*)$ /curation.php?id=$1 [L]
but when I push it to my Heroku server, I get a 500 Internal Server Error on all pages. What am I doing wrong?
Thanks

I think the main problem is you rewrite rule
try something like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]+) index.php?id=$1 [L]
</IfModule>

If you have mod_rewrite loaded, and your rules are in an htaccess file in your document root. The rules that you have are causing an infinite loop. You need to add a condition or two to prevent that:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/curation\.php
RewriteRule ^([^/]*)$ /curation.php?id=$1 [L]
Or
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /curation.php?id=$1 [L]

Related

.htaccess rewrite rule exception

I have a small question.
I have build a system and it is using a .htaccess to direct the traffic to the correct locations.
Now I have the following .htaccess data
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
RewriteRule (.*) core/$1 [L]
///### (does not work.. this is a problem) RewriteRule (.*) app/template/ !page [L]
</IfModule>
The problem is actually that when I want to show a .css file located in /app/templates it also redirects to /core/
I tried to make an exception. However that does not really seem to work at my method.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
RewriteRule (.*) core/$1 [L]
RewriteRule (.*) app/template/ !page [L]
</IfModule>
This .htaccess gives a 500 error.
Could anyone tell me what I am doing wrong? And how I can make the exception work for the template directory?
TIAD!!
You should use:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
</IfModule>

Htaccess redirect all requests to index (does not allow images) Wamp Server

Please I need a help. I am working on a site and wants to redirect all requests to an index file while allowing access to images, css, javascripts and other documents that are not php scripts. I am working on a local server (WAMP). The problem I have is that it redirects all requests to the index file including images. Below is my htaccess rule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myapp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ app/$1-$2/$3 [R=301,L]
RewriteRule ^(.*)$ index.php [L]
</IfModule>
Is this not what you're aiming for?
RewriteRule ^(.*\.php)$ index.php [L]
You should also escape those slashes in your first rewrite rule
Thanks I have figured it out, I just modified the moved the first rewrite rule above the rewrite condition and it now working fine.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myapp
RewriteRule ^(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ app/$1-$2/$3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>

mod_rewrite - Another simple one im sure

Currently making a php template/framework. Now I have done as advised and put all normal files in a PUBLIC folder with libraries and config in others and have placed the index.php into the public folder but then try and do a MOD_WRITE and nothing works - Im using the Coral8 Server (For testing) and have configured it all correctly to do it but doesn't seem to be working.
Here's what I've tried:
RewriteEngine on
RewriteRule ^public/?$ /public/index.php
RewriteRule ^public/([^/]+)/?$ /public/index.html
and this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>`
and this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
But none seem to work :(
Thank you in advance but someone tell me what I'm doing wrong so that I can learn from the mistake and then how to correct it.
Thank you
Is this what you're looking for?
RewriteEngine on
RewriteRule (.*)\.html$ /public/?action=$1&%{QUERY_STRING}$ [L]
RewriteRule ^$ /public [L]
So your URL will look like relution.co.uk/about.html
RewriteEngine on
RewriteRule ^$ /public/index.html [L]
//made addition on index.html and put it first so that it is the first rule to be applied and then makes the condition for the next rule to apply
RewriteRule (.*)\.html$ /public/?action=$1&%{QUERY_STRING}$ [L]

Apache mod rewrite for dynamic pages

I'm having a problem with my mod rewrite rule that I wrote for my website, nothing seems to change as my pages URL are loading the same as before, if anyone could have a look at it and let me know if there is any problems it would be very much appreciated, thanks!
REWRITE RULE
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?art_id=$1 [L]
URL
http://www.test.com/index.php?art_slug=test
DESIRED RESULT
http://www.test.com/test
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# to externally redirect from /index.php?art_slug=test to /art_slug/test
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+/(?:index\.php|)\?([^=]+)=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# to internally forward from /art_slug/test to /index.php?art_slug=test
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/(.+)$ /index.php?$1=$2 [L,QSA]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?art_id=$1 [L]
Should work. :)
RewriteEngine On
RewriteRule (.*)/$ search.php?keyword=$1
Should work. :)

mod_rewrite Probs

I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....

Categories