How can I use mod_rewrite to sanitize my input? - regex

I'm setting up an apache virtualhost, and I want it to redirect all requests to index.php passing as a parameter the original URL if matches a specific pattern.
Considering the pattern is (quote-less) "PaTTern[0-9]", this is what I have tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(PaTTern[0-9]).*$ /index.php?$1 [NC,L]
RewriteRule ^(.*)$ /index.php [NC,L]
What I am getting is an Error 500. Any ideas?

The last line is most probably causing rewrite loop (since it is unconditional).
Since you don't want any rewrites in case the request already goes to /index.php, add another condition:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(PaTTern[0-9]).*$ /index.php?$1 [NC,L]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ /index.php [NC,L]

Related

htaccess File Rewriting API Routes

I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

mod_rewrite does not redirect to my internal url

i'm trying to solve a mod_rewrite rule for my webshop.
The URL is: localhost/myshop/category1/category2/myproduct.html
The rediret URLshould be: localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
If I try a RewriteRule like this
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
nothing happens and the redirect will not appear.
What i'm doing wrong?
my rewrite rules in .htaccess
RewriteBase /myshop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
I want that myproduct.html redirects immediatly to the configurator link, so i want to implement a RewriteRule
Try re-ordering your rules:
RewriteEngine On
RewriteBase /myshop/
RewriteRule (^|/)myproduct\.html$ /myshop/configurator/product/configure/id/1/s/myproduct/category1/5/ [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

rewrite rule for WordPress 3.3 permalinks is not working

Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$

ajax file not loading because of trailing slash

I have an ajax file that is called when someone begins to type in search bar. I have recently been cleaning up my urls and removing file extentions adding trailing slashes, since then my ajax file doesnt appear to load anymore. can anyone help? here my htaccess so far
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
Assuming that your AJAX requests go to the /includes folder, and your normal pages do not, we can modify your rules a bit so that they look like this (including Cags' comment about the RewriteCond):
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
# We'll do the redirect first, so no other rules get in the way
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# Make sure the request didn't start with "includes"
RewriteCond %1 !^includes/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I also think that you either wanted /? at the end of the rules in the center block, or /$1/ as the replacement on the rule in the first block, so that the redirect from /page.php to /page gets interpreted correctly after the first redirect (I think right now you'll get redirected twice, once by the first block, and again by the last block).