Conflict in rewrite rules htaccess - .htaccess

This is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ search-product.php?st=basic&product=$1 [N]
RewriteRule ^([a-zA-Z0-9_-]+)-([a-zA-Z0-9_-]+)$ search-product-city.php?st=basic&product=$1&city=$2 [N]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$3 [L]
</IfModule>
The issue is only one rule works. All three files are there in the root directory. How to make all rules work. Thank you for help.

To identify different PHP files, it requires a unique prefix in the htaccess rule in order to redirect to specific file. For e.g. for search-product page searchproduct. Now your web URL would be
https://DOMAINNAME.com/searchproduct/PRODUCTID.
Similarly, you can retrieve other pages as well. Try the following code.
RewriteEngine On
RewriteRule ^/?searchproduct/([a-zA-Z0-9_-]+)$ search-product.php?st=basic&product=$1 [N]
RewriteRule ^/?searchcity/([a-zA-Z0-9_]+)-([a-zA-Z0-9_]+)$ search-product-city.php?st=basic&product=$1&city=$2 [N]
RewriteRule ^/?profile/([a-zA-Z0-9_-]+)$ profile.php?user=$3 [L]
with this, you can add as many as htaccess rules you need.

Related

How to redirect from /senturia-nam-sai-gon/?gallery=170 to /senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/?

I want to redirect https://senturia.com.vn/senturia-nam-sai-gon/?gallery=170
to https://senturia.com.vn/senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/
I tried this .htaccess file
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} gallery=170 [NC]
RewriteRule (.*) /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L]
</IfModule>
But it's not work
Your configuration looks fine. I highly recommend making sure that mod_rewrite is enabled on the server. Also, make sure that the .htaccess file is located in the correct place. If your are using some CMS with a single index.php file, the .htaccess should sit on the same level as that file. If senturia-nam-sai-gon is an actual directory on your server with its own index.php file, then the .htaccess should sit in that folder.
Here is how I would do it in a CMS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)gallery=170 [NC]
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC]
</IfModule>
Detailed break-down:
RewriteEngine on simply enables URL rewriting
RewriteBase / sets the base of all URLs (in this case simply /)
RewriteCond %{QUERY_STRING (?:^|&)galler=170 [NC] applies the following rule, if the GET parameters contain gallery=170. The [NC] flag makes the condition ignore upper- and lower-case
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC] redirects /senturia-nam-sai-gon/ to /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/ with a status-code of 301 ("Moved permanently"). The ? at the end of the rule removes the querystring entirely. The L flag makes sure this is the last rule that gets applied. The NC flag does the same as before.

htaccess rewrite rule with param before filename

I'm not able to get my rewriting rules to work. I've read tutorials but nothing explains clearly how i can use a param before my filename when i'm not calling the root index. It seems that my index rule overides all the other ones.
I want my index to show galleries according to specific tags, so all kind of links looking this way would go to index.php
www.mawebsite.com/en/tag/karate = www.mawebsite.com?lang=en&tag=karate
this part is working fine and my gallery load as i wish. But then i want to be able to use other files.
www.mawebsite.com/en/video/videoid/video-title = www.mawebsite.com/video.php?lang=en&guid=videoid&title=video-title.
Here how my htaccess file looks so far.
EDIT my file:
AddCharset UTF-8 .html
ErrorDocument 404 /notfound.html
RewriteBase /
RewriteEngine on
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]
RewriteRule ^fr/video/guid/title$ video.php [L]
RewriteRule fr/tag/([^/]+/.+)$ /?=$1 [L,QSA]
#RewriteRule ^([^/]+)$ index.php?p=$1 [L]
Redirect 301 /css/file.css /css/file.php
anything i tried going to video.php won't work.
Give the following rules a try:
RewriteEngine On
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]
The ordering of rewrite rules plays a very important part. Keep the more specific rules first:
AddCharset UTF-8 .html
ErrorDocument 404 /notfound.html
Redirect 301 /css/file.css /css/file.php
RewriteBase /
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L]
RewriteRule ^(\w\w)/(tag)/([^/]+)/?$ /?lang=$1&$2=$3 [L,QSA]
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]

Need change inside htaccess file

i have following htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html|.*\.css|.*\.js)$ http://www.example.com/maintenance.html [R=302,L]
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^home\.php$ index.php
RewriteRule ^home$ index.php
as you can see all, no matter which page you visit on the website it will be redirected to maintenance.html. I also need a small change that will exclude one more location from this. Since I am using Magento, I need to exclude admin area from this redirect so I think these are the urls that I need
http://www.example.com/index.php/admin_852_in
http://www.example.com/skin/
http://www.example.com/js/
http://www.example.com/media/
What rule should I add for these two urls too?
thanks!
You can tweak your exclusions like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html$|index\.php/admin|css/|js/|.*\.(css|js)$) http://www.example.com/maintenance.html [R=302,L,NC]

Redirect a .jpg using .htaccess

I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Therefore I've used
RewriteRule (^|.*?/)image/(.*)$ /$1view/$2 [R=302,L,NC]
But, still if I try to visit "example.com/image/img.jpg" it won't redirect. What am I doing wrong?
Thank you.
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Apparently the above examples are not accurate as your rule shows directories image and view can be at any level in the corresponding URL directory structures.
If that's the case, you may try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)?image/([^.]+)\.jpg [NC]
RewriteRule .* /%1view/%2.jpg [R,L,NC]
In case image and view directories are indeed at the first level as described in the examples, replace the last 2 lines with this one:
RewriteRule ^image/(.*) /view/$1 [R,L,NC]
Replace [R,L,NC] with [R=301,L,NC] for permanent redirection or with [L,NC] for internal mapping.

.htacces rewrite condition not hitting?

Lets say I have a two websites www.sample.com and files.sample.com.
In an .htaccess file within the webroot of www.sample.com, I have the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} ^files\/uploads [NC]
RewriteRule ^(.*)$ http://files.website.com/$1 [NC,L]
</IfModule>
The desired result is to have all requests of www.sample.com/files/uploads/file.xml or www.sample.com/files/uploads/subfolder/file.json get 302 redirected to files.sample.com/files/uploads/file.xml and www.sample.com/files/uploads/subfolder/file.json, respectively.
However, I can't get the rule to fire. The directory "files" does not exist on the www.sample.com website at all.
Could anyone give me a little help as to why the
You probably want REQUEST_URI not QUERY_STRING.
RewriteCond %{REQUEST_URI} ^/files\/uploads [NC]
Also note the leading slash.
Did you turn on mod_rewrite in your apache config?
Also yor change '^files/uploads' to ^/files/uploads and QUERY_STRING to PATH_INFO.
QUERY_STRING - this is all data after '?' character.

Resources