HTAccess Rewrite Needed - .htaccess

I need to rewrite any url on my domain that starts with **/club/***anyPage* to /page/club/anyPage.
For this, I believe I can use the following command:
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
Also, if the physical page does not exist I want it conditionally rewritten like so:
/club/$1 to /page/club/club.php?title=$1
So in this case if the user enters in mysite.com/club/New_Club they should actually be viewing /page/club/club.php?title=New_Club

Your original rules seems to work OK for it's job, but to encorporate the second requirement, something like this should do the job...
# do first rule if not file/directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^club/([^/]*)$ /page/club/club.php?title=$1 [NC]
# do second rule if not file/directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
or depending on other rules...
# prevent requests for real files / directories from rewritting at all
RewriteCond %{REQUEST_URI} -f
RewriteCond %{REQUEST_URI} -d
RewriteRule .* - [L]
RewriteRule ^club/([^/]*)$ /page/club/$1 [NC]
RewriteRule ^club/([^/]*)$ /page/club/club.php?title=$1 [NC]

Related

Htaccess - Exclude a word from existing rewrites

I have the following in my htaccess file:
# drop tags
#RewriteCond %{THE_REQUEST} (.*)designs/(.*)/?tag=shirts [NC]
#RewriteRule .* /designs/%2/ [R=301,L]
RewriteCond %{THE_REQUEST} (.*)designs/([^?]+)\?tag=[^&]* [NC]
RewriteRule .* /designs/%2? [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
My theme supports a blog but after enabling it (it defaults to example.com/blog/), when I click on the blog link in my menu, it takes me to a page that says "There is no product that matches the search criteria". When I remove the htaccess rules listed above, the blog page (which contains the article listings) works fine so it is definitely that.
How can I exclude the word "blog" from the htaccess rules so this issue disappears?
I managed to fix it after a couple hours of tinkering. The solution was:
# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
This solution also handles /blog (without the trailing /).
Hope that helps someone in the future.

Using backreference after RewriteCond in htaccess

I have:
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC]
RewriteRule ^(.*)$ index.php?q=search&keyword=$1
Input:
my.domain.com/foo_bar
I want:
index.php?q=search&keyword=foo_bar
But in fact:
index.php?q=search&keyword=index.php
I don't understand why. Please help me!
Your rewrite rule is actually rewriting twice, once for /foo_bar and second time for index.php as .* matches anything.
You just need to add 2 conditions to stop rewrite for files and directories:
# handle landing page
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteRule ^/?$ index.php?q=search [L,QSA]
# handle /foo_bar
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=search&keyword=$1 [L,QSA]

.htaccess Multiple subdirectory rewriting to www-root

I have the following folder structure:
www-root
Backend
Config
Etc
Frontend
Administration
StoreFront
I would like to be able to access the directories from the main url and hiding the subdirectories in between.
So the administrative part I should be able to access like this:
http://localhost/Administration/
The main page which is stored in the subdirectory "StoreFront", I want to be able to access from the root:
http://localhost
This is the code in my .htaccess file so far:
# Store Redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/StoreFront/$1
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^(/)?$ /Frontend/StoreFront/index.php [L]
RewriteCond %{REQUEST_URI} !^/Administration
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/Administration/$2
This code however does not work correctly. It rewrites every file except the index.php file to the Administration subdirectory. One side note: php files which are in the backend directory should remain "includable" from the frontend.
Let me tell upfront you that what you're trying to achieve is mission impossible, now let me tell you why. You have this rule:
RewriteRule ^(.*)$ /Frontend/StoreFront/$1
and down further you have:
RewriteRule ^(.*)$ /Frontend/Administration/$2
You cannot have .* going to both the places. You need to distinguish these 2 paths somehow.
It is besides the point that you have other problems also e.g.:
Not using L (LAST) flag wherever needed
Using $2 instead of $1 in 2nd rule
EDIT: Based on your comments:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(Administration(?:/.*|))$ /Frontend/$1 [L,NC]
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^$ /Frontend/StoreFront/index.php [L]
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/StoreFront/$1 [L]

Issue changing a word in a URL using mod_rewrite

I'm trying to change the word "portfolio" in my URLs to "portfolio/project" and inadvertently created a redirect loop. Would appreciate any help in pointing me in the right direction.
Example:
http://www.example.com/portfolio/interactive/abc/ to
http://www.example.com/portfolio/project/interactive/abc/
Current htaccess (last two lines relates to issue):
redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sitemap.php
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]
Your problem is that your regex also matches your target, so after the redirect, the URI matches the same rule and gets redirected again (you may have noticed that there's a bunch of /project/project/project/project/project/project/project in the URI)
Add an exclusion condition:
RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]

.htaccess redirect main domain?

How can I redirect requests to mydomain.tld/somepage.ext to mydomain.tld/mydomain/somepage.ext? I have subdomains like subdomain.mydomain.tld that I don't want to be affected by this.
I can't seem to get it to work right. I'm trying this:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite.com
RewriteCond %{REQUEST_URI} !^/mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /mysite/$1
But it isn't redirecting anything at all.
I also want to exclude one or two folders from this rule.
This seems to be working...
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysite/ # but I don't see why this line is needed when I've used [L] below
RewriteCond %{REQUEST_URI} !^/ignored_folder/
RewriteRule .* /mysite/$0 [QSA,L]
At least for now.

Resources