Htaccess not working for my domain sub folders alone - php

I have used so many codes to redirect my sub folder url, but not working.
From :: http://www.mydomain.com/doctor/searchresult.php?txtcity=Chennai&txtarea=Annanagar
To :: http://www.mydomain.com/doctor/Chennai/Annanagar
The used htaccess codes are given below
RewriteRule ^doctor/([a-zA-Z0-9_-]+)/searchresult$ /doctor/searchresult.php?txtcity=$1&txtarea=$2 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/doctor/$ searchresult.php?txtcity=$1&txtarea=$2
RewriteRule ^doctor/([a-zA-Z0-9_-]+) /doctor/searchDocResult.php?selFindDocCity=$1&txtFindDocAreaRzip=$2 [L]

Assuming you are just trying to redirect the first URL to the second listed URL, this is works on my server.
One thing to note is that the order they are listed does make a difference. The most specific need to be listed first, such that all the txtarea's within a txt city must be listed first per my example below.
Good luck let me know how it works out for you.
Tim
Tim#mpact-media.com
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com [NC]
RewriteCond %{REQUEST_URI} searchresult\.php
RewriteCond %{QUERY_STRING} txtarea=Annanagar
RewriteRule .* doctor/Chennai/Annanagar/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com [NC]
RewriteCond %{REQUEST_URI} searchresult\.php
RewriteCond %{QUERY_STRING} txtcity=Chennai
RewriteRule .* doctor/Chennai/? [R=301,L]

In order to make any .htaccess work, you need to do two things.
First thing is to enable it, in the Apache config file, inside the <Directory> directive that matches your path, the AllowOverride. For instance:
<Directory />
Options None
AllowOverride All
Order deny, allow
Deny from all
</Directory>
This will allow to override at the "/" (webroot) with the things specified in the .htaccess file. Here's the complete spec for AllowOverride directive.
Second thing will be to place inside the .htaccess any valid directive.
Remember that the RewriteCond rules stack up eachother, until you put a RewriteRule directive. This is how you will be able to make more complex rules. You'll find a nice guide for this here.

Related

Rewrite GET color variation variable in Wordpress/Woocommerce

I was trying all the solutions I could find on StackOverflow, but still was unable to make it work. So, my last hope is to ask this question directly.
So, I have a wordpress/woocommerce shop with variable products that have colors. As you probably know, you can access specific variations with a GET variable. For example:
https://example.com/shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/?attribute_pa_product-color=chestnut-burnished
"chestnut-burnished" is, of course, dynamic value as there are plenty of other colors.
What I would like to do is to be able to access this variation by entering this URL instead:
https://example.com/shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/chestnut-burnished/
I've been trying lots of rewrite rules, but none seem to be working. If I input the URL above I always get a 404 page instead of the single product color variation one.
I guess this, has something to do with the existing Wordpress rule, which is in root .htaccess and makes pretty links. Here's the full .htaccess file that I have:
User-agent: Yandex
Disallow: /
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^summer\-sale/ /mens-shoes-sale/? [R=301,L]
RewriteRule ^summer\-sale /mens-shoes-sale/? [R=301,L]
RewriteRule ^blog/shoes/10\-most\-popular\-mens\-classic\-shoes/$ /blog/mens\-shoes/10\-most\-popular\-mens\-classic\-shoes/? [L,R=301]
RewriteRule ^blog/shoes/how\-to\-care\-for\-your\-leather\-shoes/$ /blog/mens\-shoes/how\-to\-care\-for\-your\-leather\-shoes/? [L,R=301]
RewriteRule ^blog/shoes/6\-classic\-shoes\-every\-man\-should\-own/$ /blog/mens\-shoes/6\-classic\-shoes\-every\-man\-should\-own/? [L,R=301]
RewriteRule ^blog/shoes/how\-to\-choose\-the\-right\-black\-tie\-tuxedo\-shoes/$ /blog/mens\-shoes/how\-to\-choose\-the\-right\-black\-tie\-tuxedo\-shoes/? [L,R=301]
RewriteRule ^blog/shoes/5\-british\-made\-shoes\-worn\-by\-movie\-characters/$ /blog/mens\-shoes/5\-british\-made\-shoes\-worn\-by\-movie\-characters/? [L,R=301]
RewriteRule ^blog/shoes/6\-most\-popular\-mens\-brogue\-shoe\-styles/$ /blog/mens\-shoes/6\-most\-popular\-mens\-brogue\-shoe\-styles/? [L,R=301]
RewriteRule ^blog/shoes/5\-benefits\-of\-a\-goodyear\-welted\-shoes/$ /blog/mens\-shoes/5\-benefits\-of\-a\-goodyear\-welted\-shoes/? [L,R=301]
RewriteRule ^mens-shoes/loake/?$ /mens-shoes/?brand=loake&orderby=menu_order [NC,L,QSA]
RewriteRule ^mens-shoes/carlos-santos/?$ /mens-shoes/?brand=carlos-santos&orderby=menu_order [NC,L,QSA]
RewriteRule ^mens-shoes/trickers/?$ /mens-shoes/?brand=trickers&orderby=menu_order [NC,L,QSA]
RewriteCond %{QUERY_STRING} ^post_type\=product&p\=5979$
RewriteRule ^$ /shoe-care/brushes/? [R=301,L]
RewriteRule ^/?shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/([^/]+)/?$ /shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/?attribute_pa_product-color=$1 [QSA,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?wp\-content/+debug\.log$
RewriteRule .* - [F,L,NC]
</IfModule>
<IfModule !mod_rewrite.c>
<Files "debug.log">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
</IfModule>
# BEGIN LSCACHE
# END LSCACHE
# BEGIN NON_LSCACHE
# END NON_LSCACHE
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any chance you could help me?
This would be the required rewriting rule when implemented on top level:
RewriteRule ^/?shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/([^/]+)/?$ /shop/mens-shoes/derby-shoes/trickers-wiltshire-leather-derby-shoes/?attribute_pa_product-color=$1 [QSA,L]
You should prefer to implement such a rule in the actual http server's host configuration. If you do not have access to that (read: you are using a cheap hosting provider), then you can implement it in a distributed configuration file, typically called ".htaccess". That file would have to be placed in the root folder of your http host (DOCUMENT_ROOT).
Most likely there already is such a file which already contains rules, so you need to integrate this one. Keep in mind that the order of processing those rules is top to bottom and the order is relevant. Typically you'd have to implement such an exception rule further up, above the generic rules generated by your framework.
It might also be possible to implement such a rule further down in the folder hierarchy. But that depends on your actual setup you did not share with us.
Also you might want to implement a more generic rule which applies to multiple products instead of just a specific on. But again: there isn't enough information in the question to give a straight forward answer to that.
You wrote that you already tried all existing answers to this question you found here. And I assume you also read into the documentation of apache's rewriting module, since it offers good explanations and great examples.
Since you failed to get things to work it might well be that there is a general miss understanding here. But once more this is just a guess due to the lack of detailed information in the question.

.htaccess not excluding sub directory with rules

I'm asking a question regarding my .htaccess. I'll quickly explain what i'm trying to accomplish and how it's not working.
I'm using the $_GET directory as a rule for the page.
i.e https://example.com/test?p=page1 = https://example.com/test/page1
I also have a directory called "account" inside this /test/ directory but when I try to access the page.
https://example.com/test/account it redirects me to https://example.com/test/account/?p=account
note (https://example.com/test/account/ works fine)
I've tried disabling it by following methods on other StackOverflow posts but none of this is working.
My rule is:
RewriteRule ^([0-9a-zA-Z-_\ ]+)/?$ index.php?p=$1 [NC,L]
Is there any way I can configure this rule to exclude the word "account" and "admin" etc I've tried placing a .htaccess with the rewrite engine off but that doesn't work because the .htaccess thinks /account is a page rather than a directory.
I've tried other methods which stop directories with the same issue. It doesn't think /account is a directory. It thinks it's a page.
Any help would be great!
add this line above to exclude requests with account or admin in them
RewriteCond %{REQUEST_URI} !(account|admin)$ [NC]
put that above your current rule and it should exclude using the rule on requests including those terms.
here is what I use (have for ages), to do what you are wanting to do:
**I am pretty sure I could reduce this to a single set of expressions but I'm not that good. Without the second I miss domain.tld and without the first I miss fancy.domain.tld
ErrorDocument 404 http://domain.tld
RewriteEngine On
RewriteCond %{REQUEST_URI} !(actionspage|\/excludeddir\/|\/otherdir\/).*$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.tld$ [NC]
RewriteRule ^(.*)$ index.php?pre=%1&pos=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !(actionspage|\/excludeddir\/|\/otherdir\/).*$ [NC]
RewriteRule ^(.*)$ index.php?pre=0&pos=$1 [L,QSA]

Specific redirection in .htaccess does not work

I have a specific problem with my mod_rewrite configuration that I cannot resolve. I am no admin, therefore I'm kindly asking for a collective advice :) Please note - it's not a general question about redirection, but very specific one.
Story
I have a shared hosting with access to FTP and ability to create my own .htaccess files. This shared hosting had plenty of files and directories before I created the website, so logical step for me was to place everything inside new-site folder.
Then I had to create custom rewrite rules so that everything under example.com points to new-site.
CONFIG
So I came up with the following config.
# (...) other rules
# 1. Make sure that /new-site/ is not a duplicated content
RewriteCond %{REQUEST_URI} ^/new-site/
RewriteRule ^/new-site/(.*)$ /$1 [R=301,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_URI} !^/new-site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new-site/$1
RESULTS
Rule marked with 2. works fine, my site is accessible as I want. However I didn't want https://example.com/new-site/ to be found on the server by bots and treated by a duplicated content, so I added rule 1..
This rule, however, doesn't seem to have any effect! I looked it up with CURL and request is handled immediately with a 200 status. I'm banging my head against the wall and experimenting with other variants of it, but everything fails.
What I'm after is pretty darn simple:
Make every request to the root domain be handled by website which is stored in /new-site/
Make sure that direct call to https://example.com/new-site/(.*) is redirected with 301 status back to the domain root.
What am I doing wrong?
EDIT
I've noticed that my setup seems to be doing far better if I remove a child .htaccess file under /new-site/ subfolder. I didn't mention it in my original question because there is nothing special about it (just some SEO rewrites).
RewriteEngine on
DirectoryIndex index.php
RewriteRule ^products$ products.php
# (...) similar rewrites
Old answer: RewriteRule does not accept leading slash. Try to change to
RewriteRule ^new-site/(.*)$ /$1 [R=301,L]
Edit:
Version that is provided by you will forward to the cyclic redirection. To avoid it, I think, you can use such .htaccess
RewriteEngine on
RewriteRule ^new-site/ - [R=404,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_URI} !^/new-site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new-site/$1 [L]
Direct asking /new-site/* wil receive 404 error, while url exaple.com/* wil be redirected to /new-site
And notice that if there are files with the same name, for example, /r.jpg and /new-site/r.jpg, the last never be achieve
Your first rule never matches because it must not begin with a leading slash.
With RewriteRule, you only need a leading slash if you're directly in httpd.conf or before Apache v2.4 i think.
While you have a good idea, your first rule will cause an infinite redirection loop if it's working. You have to use THE_REQUEST to match direct user request only.
You can put this code in /.htaccess
# 1. Make sure that /new-site/ is not a duplicated content
RewriteCond %{THE_REQUEST} \s/new-site/([^\s]*)\s [NC]
RewriteRule ^ /%1 [R=301,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!new-site/).*)$ /new-site/$1 [L]
Also, you'll have to add this line in /new-site/.htaccess (to avoid automatic override)
RewriteOptions InheritBefore

Htaccess, Directory-Style not rewriting to query-style

I have a very basic htaccess, attempting to convert directory-style to query style.
i.e.
a. /public_html/main/folder1/folder2 ... etc
b. /public_html/main.php?1=folder1&1=folder2
No matter what regex I try, to matter what rewrite rule I try - I can't make any progress on this problem.
I've searched the web, many times so far. I've tried 10 different 'recommended' solutions found in other forums.
I don't know where else to look.
Here's my current htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^public_html/([A-Za-z0-9]+)/([0-9]+)/?$ main.php?name=$1&page=$2 [NC]
Here's my httpd.conf Htaccess directives.
(Directory "/Users/admin/Sites")
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
(/Directory)
(Directory "/Users/admin/Sites/MFCS/V3/public_html/mfcs/public_html")
AllowOverride All
(/Directory)
I replaced the greater than / less than on the directory tags for this example.
the httpd.conf does have the proper tags in it.
Give this a try:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/main/(.*)/(.*)/?$ /main.php?name=$1&page=$2 [L]
This will redirect requests to:
http://www.example.com/main/folder1/folder2
To:
http://www.example.com/main.php?name=folder1&page=folder2
If this is not the outcome your looking for please provide a sample before and after. On another note did you make sure to enable mod_rewrite? I also removed the public_html from the URI because it's normally not visible to the public.

.htaccess in subdirectory 'overriding' parent htaccess

been searching for 2 days and can't quite get the right solution due to my lack of understanding of mod_rewrite and time constraints on this project so hoping someone can help.
The aim
To rewrite all requests to the root index.php if the client doesn't have the correct cookie.
If the client has the correct cookie allow them to browse as they wish.
The problem
The htaccess in my subdirectory is taking precendence over my root htaccess, so requests such as www.mydomain.com/subdir/index.php arn't getting redirected.
My root .htaccess
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*pass.*$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?url=$0 [NC]
My subdir htaccess
RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Additional info
Ideally I'm trying to create a password protected area, so all requests are routed to index.php where a password can be entered and when verified a cookie is created, allowing free browsing of contents and sub directories. So if there is a better way to accomplish this then please let me know, and I havn't gone for .htpasswd since I need custom login, error and splash pages.
Also, the subdir .htaccess is an ExpressionEngine URL handler.
Thanks.
To allow execution of rewrite rules from parent .htaccess (htaccess from parent folder), you need to explicitly allow it (Apache will treat rewrite rules in current .htaccess as the only one that need to be executed, as long as rewritten URL remains in the same subfolder).
You need to add this line to your .htaccess in sub-folder:
RewriteOptions inherit
Apache manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

Categories