Having troubles with url-rewriting using .htaccess on wamp server - apache

I'm trying to rewrite the url of a certain php file.
Aim for url: www.mysite.com/filename/topic/ or www.mysite.com/filename/topic
What works: www.mysite.com/filename and www.mysite.com/topic
I've tried all rewrite rules I know which should work, but every time I add the topic to the url I end up with a 500 internal server error
Current .htacces file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
//No need for php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
//Rewriting the url
RewriteRule ^([a-zA-Z0-9_-]+)$ filename.php?topic=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ filename.php?topic=$1

You have conflicting rules and there is probably an issue with content negotiation with MultiViews. Also you aren't using all the flags ([L]) need to prevent continuation of rules matching.
You can't do it that way with the second set of rules and you don't need 2 rules just to have a / or not. You can make it optional. You need to do something like this.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?topic=$2 [L]
</IfModule>

Related

How to fix mod_rewrite problem with static to dynamic content

I would appreciate if someone can help with this mod rewrite issue. For years I had this in my htaccess, apache include files and it worked just fine.
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
But recently it stopped working. Browser shows "No input file specified." and access log shows 404 response code the requests that had to be fulfilled by the condition above.
RewriteEngine is on. This is what my htaccess mod_rewrite rules at the moment
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) /404error.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
RewriteRule ^/relationships/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
</IfModule>
With your shown samples/attempts, could you please try following htaccess Rules. Please make sure to place your htaccess rules file in root and clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /
##Rules for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
##Rules for articile and relationship uris here.
RewriteRule ^(?:article|relationships)/([0-9]*)\.html/?$ article/article.php?id=$1 [NC,L]
##Rules for 404 handling.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) 404error.php [L,NC]
</IfModule>

htaccess is supposed to remove index.php from current URL, but instead redirects to root

I've read been reading StackOverflow posts for the last 30 minutes and none of them work. Maybe there is a server setup that is preventing this from working?
I just want to remove index.php from whatever URL is typed in. For example, www.mysite.com/blah/blah/index.php would become www.mysite.com/blah/blah/. www.mysite.com/index.php would become www.mysite.com/.
I've read at least 10 posts and tried each one, but it ALWAYS just redirects to the root. Here is the current code I'm using that looks like it should work:
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Instead of just removing index.php from the URL, it redirects to the root of the site.
I think this is what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
</IfModule>

Trying to apply mod rewrite rule for e.g. css/style.123456.css -> css/style.min.css

I'm trying to create a rewrite rule for any js/css files with a given time stamp to point to the real files but not really sure how to do it. Below is my attempt but when I try this it seems to mess up my
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css)\/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
By the way, my folder structure is as follows:
/public/
/public/index.php
/public/.htaccess (rules are written in this file)
/public/css/style.min.css
/public/js/all.min.js
Also, the apache vhost conf file will direct requests to the /public/ directory. This I can verify works. But when I try to view /css/style.12235543543.css (which I want to point to /css/style.min.css) it just goes through the front controller (index.php) instead of the stylesheet.
Use that:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(js|css)/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4 [L]
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

Why are these mod_rewrite rules not working?

I have an .htaccess file that looks like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>
Basically, I'm trying to write an API that can handle requests like get_all/ or get/123/.
The problem is, when I try to go to either of the above URLs, the cmd parameter is always set to index.php and the num parameter is not set at all.
If I comment out either of the RewriteRule lines, then requests work for the remaining, uncommented out RewriteRule, but I need to be able to handle both cases.
I am aware of the looping that occurs with mod_rewrite, but in this case, I have no clue how to stop it. I can't even understand why the above rules are causing cmd to be set to index.php.
Can anyone please explain what is happening here and how to fix it?
Thank you.
Rules are behaving because RewriteCond is only applied to very next RewriteRule. Due to this your last RewriteRule is running without conditions and hence running twice by mod_rewrite loop.
Use this code to fix it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>
If you want to avoid repetition of Rewriteond then use:
<IfModule mod_rewrite.c>
RewriteEngine On
# skips files and directories from rewrite rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>

Issue with URL rewriting and parameter GET

I have some problem to build my url rewrite. On my local server, working with nginx, everything is going well. But as soon as I try to make it working for apache with htacces, I kind of get crazy.
My urls are looking like this:
http://example.com/controller/action/param1/value1/param2/value2
Following is the htaccess working for the previous case:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
But in some case I have some extra parameter, for example for Facebook authentication:
http://example.com/controller/action?hauth.start=Facebook&hauth.time=1382665465
This case is not working with the previous htaccess. Nothing is going in the GET variables. How could I fix this?
Use QSA (Query String Append) flag in your rule
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
</IfModule>
QSA flag makes sure to preserve an existing query string while adding new query parameters.