htaccess rewrite subfolders - .htaccess

I want to rewrite two virtual subfolders /content/view/, and /content/section/. I now have this:
RewriteCond %{REQUEST_URI} ^htaccesstst/content/(view|section)+
RewriteRule ^$ http://www\.google\.nl [L,R=301]
This rewrites /content, /content/, /content/view/, /content/section/, which is correct. BUT it rewrites /content/ (with nothing after it) too. I don't want this! Help?

You can replace it with:
RewriteRule ^htaccesstst/content/(view|section)/?$ http://www.google.nl [L,R=301]

Related

.htaccess - Rewrite query string and redirect to directory

I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>

rewrite rule to remove folders in url

when it come to ht-access, i am not very good at this. i tried reading but cant understand it well
i have my website: http://www.website.com/folder1/folder2/param
and i need to rewrite it into: http://www.website.com/folder2/param
can anybody help me with this and tell me what each line is actually doing.
i need to rewrite only if folder2 is the next folder and not other folders
so:
/folder1/folderxxx/param
will remain as it is.
i did this:
RewriteCond %{REQUEST_URI} !([a-z]+)/folder1.php
RewriteCond %{REQUEST_URI} ^/folder1.php$
RewriteRule ^(.*)$ folder/folder1/ [L]
and when i go to www.website.com/folder.php i get the page but i don't need the .php and if i remove it it doesnt work
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^folder2/(.*)$ /folder1/folder2/$1 [L]
And you may need this to remove it from the URL in the browser's locaiton bar:
RewriteCond %{THE_REQUEST} \ /folder1/folder2
RewriteRule ^folder1/folder2/(.*)$ /folder2/$1 [L,R=301]

.htaccess rewrite rule for dynamic sub folders

I am trying to rewrite rule in htaccess for subfolders created dynamically /folder/folder/folder/folder/index.php?product=car
i want like this folder/folder/folder/folder/product/car
code
RewriteCond %{QUERY_STRING} ^product=1$
RewriteRule ^([^/]+)/? index.php?product=$1 [L]
If I understand you correctly
RewriteEngine On
RewriteRule ^((?:[^/]+?/)*)product/(.+)$ $1index.php?product=$2 [L,NC]

301redirect to remove folder from URL

I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]

Remove subdirectory & remove .php extention in htaccess not working?

I m new to url rewrite:
First, here is what I am trying to accomplish:
Current URL: www.example.com/subdirectory1/subdirectory2/something.php
Desired URL: www.example.com/subdirectory1/something/
And, the name of subdirectory2 is fixed.
Possible?
My current htaccess just to remove the ".php" but also not working. (Any idea how to debug htaccess??)
RewritEngine on
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(?!subdirectory1/|subdirectory2/)(.+)$ subdirectory1/$1 [L]
Thanks.
Your first problem is RewritEngine on. You are missing an e. Should be RewriteEngine on.
Try this:
RewriteEngine on
# Remove .php
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^([^/]+)/fixed/([^/]+).php$ /$1/$2/ [R=301,L]
# Rewrite "friendly" URL into php.
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/fixed/$2.php [L]
This only works for exactly what you said. Fixed is always the same. Replace it with the correct value.
The users goes to: www.example.com/1234/fixed/5678.php. He is redirected to www.example.com/1234/5678
User goes to www.example.com/1234/5678. On the server, this becomes www.example.com/1234/fixed/5678.php.
Something like www.example.com/1234/5678/9abcd will not work. (More than two levels of directories.)

Resources