Rewrite multiple sub-directories to the same path htaccess - .htaccess

I want to rewrite multiple sub-directories to the same path. while I can remove a part from URL Apache htaccess
for example, I want
domain.com/category/computer/internet-topics
domain.com/category/computer/windows-topics
domain.com/category/science/physics-topics
domain.com/category/science/biology-topics
to be
domain.com/category/internet-topics
domain.com/category/windows-topics
domain.com/category/physics-topics
domain.com/category/biology-topics
When I use this, it works fine for the first line only (computer)
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/computer/$1
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/science/$1

I think you need to be specific else only first rule will be used
RewriteEngine On
RewriteRule ^category/((internet)|(windows)-topics)$ category/computer/$1
RewriteRule ^category/((physics)|(biology)-topics)$ category/science/$1

Related

Maintain separate .htaccess files in sub-folder

How do I maintain 2 separate .htaccess files so as to avoid dumping rules in the main .htaccess file.
For example: I want all the urls trailing after ^shows(/.*)?$ to point to .htaccess created under controllers/shows (.htaccess)
Depending on your rewrite rules, this can be accomplished with minimal rewriting (heh) of the .htaccess file. Take for example this .htaccess file stored in the webroot /var/www/html:
RewriteEngine On
RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1
Rewriterule ^blog/(.*)? blog/index.php?id=$1
We're doing two kinds of rewrite here:
www.example.com/foo/help/123 -> wwww.example.com/foo/help.php?q=123
www.example.com/blog/myarticle -> www.example.com/blog/index.php?id=myarticle
Notice the first RewriteRule is using absolute paths, while the second is using a relative path.
If I were to move the first RewriteRule into /var/www/html/foo/.htaccess, the file would look like this:
RewriteEngine On
RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1
Note that nothing needs to change, since you're using an absolute path.
Now if I were to move the second RewriteRule into /var/www/html/blog/.htaccess, the file would look like this:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^(.*)? index.php?id=$1
Note the paths in the RewriteRule were stripped to make them relative to the current directory, which is now /blog. Also note the RewriteBase directive, which is necessary when using relative rewrite rules in a subdirectory.
Check out this question for another example of using subdirectory .htaccess rewrite rules.

.htaccess rewrite 301 rules not working

I am in the process of simplifying my URLs and have come up with a problem concerning the use of Rewrite in htaccess files.
Search engines currently list my pages in the following format
www.example.com/x/x/article_a/1159/
which is currently rewritten by the following htaccess file placed in the /x/x directory
RewriteRule walk_a/(.*)/$ /x/x/article_a.php?id=$1
This has worked fine for many years.
I want to simplify the URL to
www.example.com/article-1159-introduction
I have tried the following redirects placed in the root
RewriteRule ^x/x/article_a/(.*)/$ http://www.example.com/article-$1-introduction [R=301,NC,L]
RewriteRule ^article-(.*)-introduction$ /x/x/article_a.php?id=$1
The 301 rewrite seems to have no effect although the second rewrite command works fine.
What am I doing wrong?
Inside /x/x/.htaccess directory use this rule:
RewriteEngine On
RewriteRule ^article_a/(.*)/$ /article-$1-introduction [R=301,NC,L,NE]
Then inside root .htaccess have this rule:
RewriteEngine On
RewriteRule ^article-(.+)-introduction$ /x/x/article_a.php?id=$1 [L,QSA]

Generic .htaccess folder redirect with variable root paths

I want to do a simple redirect so that a request for app/scans/large/as89q6dfa.jpg results in app/scans/medium/as89q6dfa.jpg, etc. The trouble is that this app will be used on a few domains with different base paths. The code I've tried keeps rewriting to the absolute base path of the site and the app is actually a variable number of folders deep on the site. Is there a generalized way to do a redirect like this, without hard-coding the base path?
Here's my file and folder scructure:
app/.htaccess
app/scans/large
app/scans/medium
So the .htaccess rules should work for:
includes/app/scans/large
inc/app/scans/large
script/engine/app/scans/large
Here's my first attempt:
RedirectMatch 301 scans/large/(.*) scans/medium//$1
Here's my second attempt:
RewriteEngine On
RewriteBase /
RewriteRule ^scans/large/(.*) scans/medium/$1 [R=301,L]
When you use the "^" caracter it means that the regex will search for the string "scans/large" in the BEGINNING of the URL.
So as the base is "/", it won't work on "includes/app/scans/large/omg" because "scans" it's not in the beginning of the string.
Try this solution:
RewriteEngine On
RewriteBase /
RewriteRule (.*)scans/large/(.*) $1scans/medium/$2 [R=301,L]
It worked perfectly with the URL:
http://www.example.com/includes/app/scans/large/omg
That redirects to:
http://www.example.com/includes/app/scans/medium/omg
You can do more tests on it here: link
You can actually get RewriteBase be determined dynamically.
RewriteEngine On
# determine BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^scans/large/(.*)$ %{ENV:BASE}scans/medium/$1 [R=301,L,NC]

htaccess - rewrite URL to make querystring nice

I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)

rewrite php file in htaccess with our without query string

So I have photographs.php, and there are also sections where it will be rewritten as photographs.php?cat=somecategory
Question is, in .htaccess how do I get these both to work as such
/photographs
and
/photographs/somecategory
so that the $_GET['cat'] variable will be = somecategory
/photographs/(.+) will need to redirect to photographs.php?cat=$1, while /photographs/? will just redirect to photographs.php. if you want to be clever, you can combine it and do it in one line and /photograph will just go to photographs.php?cat=[blank].
First of all you must have the mod_rewrite module activated.
Here's what you need to add to your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^photographs$ photographs.php [NC]
RewriteRule ^photographs/(.*)$ photographs.php?cat=$1 [NC]
Just some simple replace rules. You can find lots of info on the web about them.

Resources