htaccess rewrite clean url for multiple parameters - apache

This is my .htaccess code for clean url rewriting.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[a-zA-Z]{3,}\s/+index\.php\?bank=([^\s&]+) [NC]
RewriteRule ^ %1%2%3%4? [R=301,L]
RewriteRule ^([^/]+)/?$ index.php?bank=$1$2$3$4 [L,QSA]
This code works fine for single parameter in url. For example it rewrites
http://example.com/example-path/ to
http://example.com/index.php?bank=example-path/
But when I tried to pass multiple parameters in url, all I got are errors.
I need a url like
http://example.com/example-path instead of http://example.com/index.php?bank=example-path
How can I alter My code to pass multiple urls.

Your redirect logic can be greatly simplified to just:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#If a request does not match an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
#And does not match an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite all requests to index.php
RewriteRule ^(.*)/?$ index.php?bank=$1 [L,QSA]

Related

Configuring htaccess for url rewriting

I am trying hard to rewrite this URL
http://localhost/sitecms/?search=viewPage&pageId=1
TO
http://localhost/sitecms/pages/page/1
N.B. Variable 'search' declared at the top inside index.php, viewPage as function declared inside index.php receives pageId variable, and pass to viewPage.php inside another folder(template) along with associated data.
Below is my .htaccess code.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pages/([^/]+)/?$ ?search=viewPage&pageId=$1 [L]
It's not working.
Suggestions are highly welcome about what I missed.
With your shown samples, could you please try following.
Please clear your browser cache before testing your URLs.
Options +FollowSymLinks
Options -MultiViews
RewriteEngine ON
##This rule redirects from http://localhost/sitecms/?search=viewPage&pageId=1 TO http://localhost/sitecms/pages/page/1
RewriteCond %{THE_REQUEST} \s/(sitecms)/\?search=viewPage&pageId=(\d+) [NC]
RewriteRule ^ /%1/pages/page/%2? [R=301,NE,L]
##Rules for handling non-existing files/directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(sitecms)/(?:[^/]*)/(?:[^/]*)/(\d+)/?$ $1/?search=viewPage&PageId=$2 [NC,L]

.htaccess rewrite exception for letsencrypt doesn't work

I have a custom application that needs to redirect all requests through a central router, and I need to add an exception for LetsEncrypt to make requests to the temporary /.well-known directory created directly beneath the document root.
Say my original .htaccess file is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^.*$ router.php
I modified it to read:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/.well-known.*$
RewriteRule ^.*$ router.php
But I can see requests still being forced into the router. Why doesn't this rewrite condition work?
You may use this rule:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/\.well-known/?[?\s] [NC]
RewriteRule ^ router.php [L]
Using additional RewriteCond to skip files and directories and using THE_REQUEST instead of REQUEST_URI.

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

Symfony2: simple rewrite rule in vhost to strip .html extention out

Since I migrated my project on Symfony2, I also avoided to use .html extensions previously used for my generated urls.
So, after looking at some examples, I'am trying to execute this in my /etc/apache2/sites-available/mysite:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1 [NC,L]
#this is the standard rewrite rule to redirect every request to /web/app.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I have tried many combinations but no one seems to work. Any Help? Thanks

URL Rewrite Rule using .htaccess not working as expected

In continuation with my previous Question url-rewrite-rule-based-on-certain-conditions
I am trying to do a URL rewrite like this
http://www.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.mydomain.com/wp-content/plugins/pluginA/abc.js
i want that when the CSS and JS are from this plugin (Plugin A ) or any other plugin whose URL i want to rewrite, The URL should get rewritten as
http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.js
This is what i tried as in my .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} ^.+\.(css|js)$
RewriteRule ^wp-content/plugins/(nivo-slider-light|Usernoise|lightbox-plus|wp-content-slideshow|content-slide)/(.+)$ http:/abc.cloudfront.net/wp-content/plugins/$1/$2 [L,R=302,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
I have tried every possible way to rewrite the URL so that JS and CSS should get served from my CDN server, but seems like the rule is never been picked up by apache.Any help/pointer in this regard will really be appreciated.
Make this your first rule (right after RewriteBase line)
RewriteRule ^(wp-content/plugins/(nivo-slider-light|Usernoise|lightbox-plus|wp-content-slideshow|content-slide)/[^.]+\.(css|js))$ http://abc.cloudfront.net/$1 [L,R,NC]
Also if it doesn't work try moving your *.js and *.css files away from local directories under wp-content/plugins/ path.