htaccess RewriteRule for different files in same directory with SEO - .htaccess

I have this code so far in trying to get to the file news.php in a fixed directory name. It doesn't work, because it still loads the other one below named profile.
RewriteRule ^/?FIXED_DIRECTORY_NAME/[\w-]+/([\w-]+)/([\w-]+)/?$ /FIXED_DIRECTORY_NAME/news.php?artist_id=$1&page_id=$1 [L,QSA,NC]
The url itself is this:
domain.com/FIXED_DIRECTORY_NAME/RANDOM_NAME/RANDOM_ID/news
eg. domain.com/guitarists/joe-bonamassa/999/news
where 'news' is the actual news.php in: FIXED_DIRECTORY_NAME
The one with profile actually works, but news.php doesn't:
RewriteRule ^/?FIXED_DIRECTORY_NAME/[\w-]+/([\w-]+)/([\w-]+)/?$ /FIXED_DIRECTORY_NAME/profile.php?artist_id=$1&page_id=$1 [L,QSA,NC]
domain.com/FIXED_DIRECTORY_NAME/RANDOM_NAME/RANDOM_ID/profile
eg. domain.com/guitarists/joe-bonamassa/999/profile
We are fetching the content from the dBase on the RANDOM_ID.

Oops, found the answer after switching the two lines.
Didn't know about this structure.
RewriteRule ^/?FIXED_DIRECTORY_NAME/[\w-]+/([\w-]+)/news/?$ /FIXED_DIRECTORY_NAME/news.php?id=$1&page_id=$1 [L,QSA,NC]
RewriteRule ^/?FIXED_DIRECTORY_NAME/[\w-]+/([\w-]+)/([\w-]+)/?$ /FIXED_DIRECTORY_NAME/profile.php?artist_id=$1&page_id=$1 [L,QSA,NC]
If anyone can help me making this code better, please do!

Related

htaccess Rewrite Error, Can not fetch $_GET value PHP :(

I am sorry to open a replicated question once again but I had no other choice. I am trying to write a clean URL using .htaccess. Here is my code:
RewriteRule ^home index.php [NC,L]
RewriteRule ^about-us about-us.php [NC,L]
RewriteRule ^careers careers.php [NC,L]
RewriteRule ^contact-us contact-us.php [NC,L]
these works very finely. but when I move on to URL's having some GET params like
example.com/providers.php?provider=huawei
and the htaccess rule goes as:
RewriteRule ^providers/([a-zA-Z_-]+) providers.php?provider=$1 [NC,L]
When I navigate to the URL example.com/providers/huawei it throws an error
Notice: Undefined index: provider in directory\providers.php on line x
Appearantly, there is no error in the rule, I have gone through several video and StackOverflow solutions. Some suggested the use of QSA but no luck. I changed my production servers too still negative. Any help in this regard.
TIA

htaccess replace the parameter in the url

I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

htaccess file contains conflicting rules - multiple page types and similar rules

I have 2 types of pages that follow very similar rules. I am trying to rewrite the rules for each page type and I am running into some issues.
RewriteRule (mission|story|advertise)\.html $1.php [L]
##Symptom Pages
RewriteCond %{REQUEST_URI} !(.*)/health-food.html (condition duplicated for all category page urls)
RewriteRule ^health-conditions/([^.]+)\.html?$ php/symptom.php?symptoms_url=$1 [L]
#http://www.example.com/conditions/autism.html
##Email Pages
RewriteRule ^([^/]+)-([^/]+)/([^.]+)/([^.]+)\.html?$ php/email.php?slug=$1&region_slug=$2&vendor_Slug=$3&email=$4 [L,QSA,NC]
##Vendor Pages
RewriteRule ^([^/]+)-([^/]+)/([^.]+)\.html?$ php/vendor.php?slug=$1&region_slug=$2&vendor_Slug=$3 [L,QSA,NC]
##Listing Pages
RewriteRule ^([^/]+)-([^.]+)\.html?$ php/listing.php?slug=$1&region_slug=$2 [L,QSA,NC]
#http://www.example.com/allergy-specialists-new-york.html
##Category Pages
RewriteRule ^([^/.]+)\.html?$ php/category2.php?slug=$1 [L,QSA,NC]
#http://www.example.com/allergy-specialists.html
The 2-word states (listing pages) cause the wrong file to load. http://www.example.com/allergy-specialists-new-york.html does not work but http://www.example.com/allergy-specialists-ohio.html does work.
I have tried switching the order and that only causes othe pages to pull the wrong file. Please help.
Thank you
The easiest way to fix this was to add the word "in" into the Listing Page urls (ie: example.com/allergy-specialists-in-new-york.html). This corrected any conflicting htaccess rules.

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources