.htaccess rewriting rule rewriting css files - apache

I have a rewrite rule to make pretty url but its rewriting css, js and other files too.
I want to rewrite
http://localhost/example/post/?postid=af1ub4zu4y&title=this-is-the-first-post.html
to
http://localhost/example/post/af1ub4zu4y/this-is-the-first-post.html
I am trying this way
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
also tried like these
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
and
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
But none of these solution are working for me. Please see and suggest any possible way to do this.

Try placing this rule in /example/post/.htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /example/post/
RewriteRule ^([^/]+)/([^/.]+\.html?)$ ?postid=$1&title=$2 [QSA,L,B,NC]

Related

Can we write .htaccess for two different dynamic pages like product.php & blog.php in php mysql?

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?-([^/]+)? product-details.php?pro_slug=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?-([^/]+)? blog-details.php?n_slug=$1&n_id=$2 [L,QSA]
this will working for only first defined page product-details.php. please help.
I am hitting on www.test.com/blog/Want-to-transfer-your-home-loan-to-another-bank, www.test.com/product/agriculture-land-near-by-agra First url should be served by product-details and 2nd one should be served by blog-details.php file.
Based on your shown samples, please try following htaccess Rule file.
Make sure to clear your browser cache before testing your URLs.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/([\w-]+)/?$ product-details.php?pro_slug=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product)/([\w-]+)/?$ blog-details.php?n_slug=$1&n_id=$2 [NC,L,QSA]

Convert dynamic urls to static for directory listener ("Encode Explorer")

I try to convert dynamic url to static for this directory listener: http://encode-explorer.siineiolekala.net/
Demo: http://encode-explorer.siineiolekala.net/explorer/index.php
The dynamic url is:
http://files.onitex.tmweb.ru/?dir=directory/subdirectory
What I want:
http://files.onitex.tmweb.ru/directory/subdirectory
I read documentation and wrote following rule in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([\/\A-Za-z0-9-]+)/?$ /?dir=$1 [L]
It works fine in this tester http://htaccess.madewithlove.be/
but not on directory listener.
I think I need reversed rule.
Could you explain me, how I can solve this problem, please?
Update:
My current .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /?dir=$1 [L,QSA]
Use this rule in root .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /?dir=$1 [L,QSA]
Don't test it on htaccess.madewithlove.be, its not really reliable. Test it on your localhost.

How to rewrite the get variables using htaccess?

I have a htaccess file that rewrites the link
http://www.example.com/profiles.php?view_user=test
to
http://www.example.com/profiles?view_user=test
but i want to rewrite the remaining get variable --
profiles?view_user=test
to
profiles/view_user/test
help me out i dont know much of htaccess
Try adding these rules to the appropriate place in the htaccess file in your document root:
RewriteCond %{THE_REQUEST} \ /+profiles\.php\?([^=&]+)=([^&\ ]+)
RewriteRule ^ /profiles/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profiles/([^/]+)/([^/]+)/?$ /profiles.php?$1=$2 [L,QSA]
Also make sure you have multiviews turned off:
Options -Multiviews
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(profiles)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [QSA,L,NC]

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.

htaccess rewrite rule for backend/frontend app

I have a yii app that has separate back end and front end. I am trying to make friendly url.
I have tried some online tools but could not get it correct
So I want this url
http://mydomain.lc/abs/admin/some/crazy/urls (for example
some/crazy/urls can be admin/index)
will be rewritten to:
http://mydomain.lc/abs/backend.php/some/crazy/urls (because this
url works directly)
And I have also front end site but they are both in the same project so they share .httacess.
the rule for .htaccess for front end is:
this url:
http://mydomain.lc/abs/some/crazy/urls (some can not be admin
here, so we can differentiate btw fron and back end)
should be
http://mydomain.lc/abs/index.php/some/crazy/urls
I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^abs/admin/?(.*?)$ abs/backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!abs/admin\/)(.*?)$ abs/index.php?url=$1 [QSA,L]
</IfModule>
the above script is not working.
Root is under abs folder and .htaccess in the root
You do not have to change anything except RewriteBase / to RewriteBase /abs
RewriteEngine On
RewriteBase /abs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/?(.*?)$ backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin\/)(.*?)$ index.php?url=$1 [QSA,L]
I guess the problem is with the RewriteBase.
You may try this in the .htaccess file in /abs directory:
Update according to last OP comment: this one is correct: backend.php/some/crazy/urls
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /abs
# Backend admin
RewriteCond %{REQUEST_URI} !backend\.php [NC]
RewriteRule ^admin(.*) /backend.php/$1 [QSA,L,NC]
# Frontend
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_URI} !admin [NC]
RewriteRule ^(.*) /index.php/$1 [QSA,L,NC]