.htaccess Rewrite Parent Folder to Subfolder - php

I am looking to rewrite the url so if someone lands on a parent folder, they will be redirected to its subfolder. http://www.domain.com/portfolio should take me to http://www.domain.com/portfolio/example
RewriteEngine is on, and there are other RewriteRules in the file, which are working. I have tried the following rewrites, but none perform the redirect:
RewriteRule ^/portfolio/(.*)$ /portfolio/example/$1 [R=301,L,QSA]
and
RewriteCond %{HTTP_HOST} ^(www.)?domain.com/portfolio$
RewriteRule ^(/)?$ portfolio/example[L,QSA]
and
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example/$1 [R=301,L,QSA]
and
RewriteRule ^portfolio$ http://www.domain.com/portfolio/example[R=301,L,QSA]
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example$1 [R=301,L,QSA]
What is the proper syntax for rewriting a url so that if a visitor lands on the parent portfolio folder, they go to the portfolio/example subfolder?

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^portfolio(/(?!example).*|)$ /portfolio/example$1 [R=301,L,NC]

Related

Different htaccess directory and subdirectory redirect

I am trying to have clean urls on my site and here is the mapping I'm trying to achieve:
mysite.tld/directory/ is going to > mysite.tld/page.php?q=directory
mysite.tld/tags/directory/ is going to > mysite.tld/tag.php?q=directory
I have made an htaccess file at the root of my site wiht the folling code:
RewriteEngine On
RewriteRule ^([^\/]+)\/?$ pages.php?q=$1 [L,QSA]
RewriteRule ^tags/([^/d]+)/?$ tags.php?q=$1 [L,QSA]
For some reason, the tag redirect works but is then "eaten" by the other rule of redirection. Not sure what to do to prevent second rule from doing this.
Answer is as follow:
RewriteEngine On
RewriteRule ^tags/([^/d]+)\/? /tags.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/tags
RewriteRule (.*) pages.php?q=$1 [L,QSA]

Rewrite rule with exception

Here is my directory structure
main site:-
www.magentosite.com
Wordpress site:-
www.magentosite.com/wordpresssite
And Here is my htaccess rule for redirect. If User goes to wordpress site then they will redirect to magentoSite.
RewriteCond %{REQUEST_URI} !^WordpresSITE/
RewriteRule .? http://www.MagentoSITE.com/ [L]
PROBLEM
Above should work as it is but I want to put if user goes to admin side of wordpress then yes they should not redirect. In short www.magentosite.com/wordpresssite/wp-admin/... should not redirect.
The below code should go inside the .htaccess on the WordPress folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/wordpresssite/(wp-login\.php|wp-admin) [NC]
RewriteRule ^ http://www.MagentoSITE.com/ [R=302,L]
Basically what I am doing is if the URL start with:
www.magentosite.com/wordpresssite/wp-login.php
Or starts with:
www.magentosite.com/wordpresssite/wp-admin
It should not redirect, if it doesn't then it redirects.

htaccess is not working in xampp in windows

I have edited the httpd.conf file like:
changed to AllowOverride All
Here is my .htaccess file:
RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [R=301]
RewriteRule ^contact.php$ /contact/ [R=301]
RewriteRule ^shirts.php$ /shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /shirts/%1/? [R=301]
And the folder structure is like this:
shirts4mike
+ contact
+ css
+ img
+ inc
+ receipt
+ shirts
|_ shirt.php
|_ shirts.php
- .htaccess
- favicon.ico
- index.php
I've tried many times, but it still showing index file does not exits, when I click the shirts in the home page.
I think xampp does ignore the .htaccess file.
If so, How should I fix?
The problem seems to be that you try to rewrite an url in a subdirectory, but it links to the http-root directory. You need to have the .htaccess file in the subdirectory. You'll notice that a rule like RewriteRule ^test$ /testing-is-fun will match on an url http://example.com/shirts4mike/test, but rewrites to a file testing-is-fun in your http-root. We therefore need to fix the rewriting part of the rule.
The fix is having an relative url, instead of an absolute url. This can be done by removing the prefix /. For redirects, define RewriteBase with the subdirectory in it. I would add the [L] flag to every rule too. Your .htaccess should look something like this:
RewriteEngine On
RewriteBase /shirts4mike/
RewriteRule ^shirts/$ shirts/shirts.php [L]
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1 [L]
RewriteRule ^receipt.php$ receipt/ [R=301,L]
RewriteRule ^contact.php$ contact/ [R=301,L]
RewriteRule ^shirts.php$ shirts/ [R=301,L]
RewriteRule ^(shirts/[0-9]+)$ $1/ [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ shirts/%1/? [R=301,L]
As a side-note: I encourage you to do not use [R=301] while testing rewriterules. Some browsers cache a permanent redirect to increase performance. This is fine if your rules work as you expect them to work, but if that is not the case, further tests might link to your old page. This means when you change your .htaccess, you'll have to clear your cache to see the current situation.

.htaccess Pretty URL rewrite for PHP pages in a subfolder?

I've got the following directory setup:
http://www.mysite.com/public/
I'm using the following rewrite to remove the 'public' folder from visible URLs:
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteCond %{REQUEST_URI} !^/public
Rewriterule ^(.*)$ /public/$1 [L]
This works great, and redirects everything correctly. However, I also want to rewrite some of the dynamic pages that live in the 'public' subfolder as well, but am having trouble getting any of the rewrites I've found to work in conjunction with the above rule.
For example, with the above subdirectory rewrite rule in place, going to a URL like:
http://www.mysite.com/item.php?id=1&name=item_name
...should be rewritten to something like:
http://www.mysite.com/items/item_name
Thoughts?
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
Rewriterule ^(?!public/|prints/)(.*)$ /public/$1 [L,NC]
RewriteCond %{THE_REQUEST} /*item\.php\?id=([^&]*)&name=([^&]*)\s [NC]
Rewriterule ^ /prints/%1/%2? [R,L,NC]
Rewriterule ^prints/([^/]*)/([^/]*)/?$ public/item.php?id=$1&name=$2 [L,NC,QSA]
PS: Make sure your static includes like css, js, images etc have absolute path rather than a relative one.

how to write a rewriteCond in .htaccess to exclude a subdir and its subdirs

I have the following problem. I have a website and a blog in a subdirectory. Both of them are php. I have a .htaccess file in the root folder and another one in the blog folder. I dont' think is relevant, but the blog script is wordpress.
I added a condition in the root .htaccess to skip the requests made for the blog,
rewriteCond %{REQUEST_URI} !^/blog.*
Here it is how it looks. I removed the rest of the file:
Options +FollowSymlinks -MultiViews RewriteEngine on
RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
// my added line RewriteCond %{REQUEST_URI} !^/blog.*
RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(all)/([^/]+)/?$ story.php?title=$2 [L] RewriteRule ^(all)/?$ ?category=$1 [L]
RewriteRule ^/?$ index.php [L] RewriteRule ^advanced-search/?$ advancedsearch.php [L] ...
The problem I have is related to the blog requests. For example sometimes if I try to open an url it works fine, sometimes the home (root page not the blog) is opened. It seems very strange. I think it is related to the host. When the host is to busy the blog page I request is not found so the request is going to the root .htaccess.
I have 2 questions:
how to write a rule and where to
place it to exclude all the requests
for /blog to be rewritten by the root
.htaccess? the blog requests might
look like http: //test.com/blog,
http: //test.com/blog/,
http: //test.com/blog/title,
http: //test.com/blog/title/,
http: //test.com/blog/category/title
does anyone has any idea what happens? Why when I open a blog page it opens the home root page, and if I refresh the page it goes to the blog post page?
Take a look into the mod_rewrite documentation, specifically the -d flag in RewriteCond.
It should be something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule HERE

Categories