htaccess mod_rewrite URL within subfolder - apache

My question seems to be unanswered on StackOverflow, so here goes:
I want to rewrite the following URL using an htaccess file which is in the root folder.
The URL to rewrite is this:
http://www.domain.com/subfolder/item/12345
to this:
http://www.domain.com/subfolder/item.php?id=12345
However nothing I seem to do works. I can successfully rewrite the item.php URL if it is in the root folder using this:
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L]
... but not if the item.php file is in a subfolder!

You can use following .htaccess in your subfolder:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subfolder
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L,QSA]
Your rewritten URL would become: http://domain.com/subfolder/item/123

Here is an example for sending this URL (The one entered in the browser address bar):
http://www.domain.com/subfolder/item/12345 (Can be any number) to this rewrited URL:
http://www.domain.com/subfolder/item.php?id=12345
RewriteEngine on
RewriteRule ^subfolder/item/([0-9]+)/?$ subfolder/item.php?id=$1 [L]
Those directives will work only if there is a subfolder directory and in that directory there is in fact a file item.php

Related

htaccess redirect subdirectory (folder) to single page in same subdirectory

I am having another issue with .htaccess where I can't seem to find the right solution for it. What I am trying to accomplish is to redirect from a subdirectory to a specific html document within that same directory when you copy/paste the url displayed below
For example:
http://example.com/staging/test/ to http://example.com/staging/test/page.html
My (almost) working method:
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/(.*)$ test/page.html [L]
The problem with this code is that it does basically redirect to the correct page.html but if you want to click on any link on page.html it just redirects to page.html again instead of going to a particular link within subdirectory /test/ let's say /test/page-2.html
Important: This redirect should only affect the subdirectory "staging". All other sub-directories including root can not be affected.
Other info:
This code is also in my .htaccess file to remove the .html extension for the directory "staging"
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
The directory "staging" is not connected to any CMS and only consists of .html files.
I am on a shared hosting environment (Hostgator/cPanel).
Thank you.
Use below rule, you are using only L flag but I am modifying it for redirect as you mentioned.
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/$ /staging/test/page.html [R=301,L]
You want to redirect only the directoy, but not the files in it. The pattern in the rule says match the directory test/ followed by anything .*, including any files.
To match the directory alone, the pattern must stop at test, which is accomplished by an end of string anchor
RewriteRule ^test/$ /staging/test/page.html [R,L]
Instead of an absolute path, you can keep the relative path and add a RewriteBase directive
RewriteBase /staging
RewriteRule ^test/$ test/page.html [R,L]
If you want to rewrite only, leave out the R flag.

Apache invisible URL Rewrite to subdirectory

I want to use URL Rewrite to rewrite all requests on my domain to a subdirectory. But the visitor hould not see, that it is a subdirectory. I tried a view things, but nothing worked really good. Can you help me? In this subdirectory is also a .htaccess file and this file shouldn't be ignored by the server.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule (.*) sub-directory/$1 [L]

Apache htaccess Rewrite rule to append querystring to all css files

I want to append a static query string to all css files on my site. Like styles.css to styles.css?a=1. Is it possible to achieve this through rewrite rules in .htaccess file. If so what's the rewrite rule to be written. thanks in advance. .
add this to the .htaccess file under the root folder of your website, this will this redirect all css requests to the css?a=34 (query shown in the user browser), is that what you want?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)\.css$ $1.css?a=34 [R,L]
</IfModule>

How can I use mod_rewrite to redirect to a "key word" folder

It is hard to explain in title.
I want to make the following two URLs
http://www.example.com/apps/web/admin/images/pic.jpg
http://www.example.com/apps/web/blog/comment/images/pic.jpg
both redirect to
http://www.example.com/apps/view/images/pic.jpg
I put the ".htaccess" file in /apps/ folder.
No matter what is the current URL,
as long as the current URL contain the "images/",
all redirect to this folder
http://www.example.com/apps/view/images
this is my code but it cannot work
RewriteEngine On
RewriteRule ^(.*)/images/(.*)$ views/images/$2
so, how can I set the .htaccess file?
Try:
RewriteEngine On
RewriteBase /apps/
RewriteCond %{REQUEST_URI} !/view/images/
RewriteRule images/(.*)$ view/images/$1 [L]
If you actually want to redirect the browser, as in changing the URL in the address bar, change the L in the brackets to L,R=301

How to use mod_rewrite in a subdomain's .htaccess?

I have a domain (let the name be) mydomain.com.
I added a subdomain, sub.mydomain.com. (It's an other site, the two sites don't have anything to do with each other)
I have mapped the subdomain (with godaddy.com's tool) to /sub directory. It works well with static file paths, ex. http://sub.mydomain.com/js/script.js.php.
However, I would like to be able to use the rewrite module to get http://sub.mydomain.com/js/script.js
My .htaccess files:
.htaccess in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$
RewriteRule ^.*$ http://mydomain.com/sub%{REQUEST_URI} [L,P]
.htaccess in the /sub directory:
RewriteEngine on
RewriteRule ^js/script\.js$ js/script.js.php [L]
(I tried adding RewriteBase /sub/, I didn't succeed).
It seems as if Apache didn't notice the /sub's .htaccess
How can I get the rewrite work on the subdomain's paths?
SOLVED!
After a long, exhausting debugging and googling I found an Apache setting that made the trouble:
I added Options -MultiViews and voilá, it works!
/////////////////////////////////////////////
Now my configuration:
-no .htaccess in the root.
-.htaccess in the root/sub:
DirectoryIndex site/index.php
Options -MultiViews
RewriteEngine on
RewriteRule ^site/js/script.js$ /site/js/script.js.php [L]