rewrite rule to remove folders in url - .htaccess

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]

Related

What all do I need in my .htaccess, such as permissions, etc

I've got a .htaccess file at the root directory of my website, and I've made sure .htaccess is the extension, not .htaccess.txt. I'm trying to clean up my .php urls, and even after looking at all the other questions about this, it is not working.
I'm quite strongly sure that the code for removing the extension is correct, so it makes me wonder if there is other code that I need inside the .htaccess for permissions or something like that. Thanks for any answers, and please don't mark this as duplicate... I've searched for hours on Stack Overflow, SitePoint, and all the other websites.
Here is the only code inside my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Just to make sure it's clear, I want the urls:
website.com/about.php
website.com/home.php
website.com/blog.php
to look like
website.com/about
website.com/home
website.com/blog
It should remove .php from ALL .php files in all directories for my website.
you actually want
RewriteEngine On
#if the requested file doesn't exist on server filesystem
RewriteCond %{REQUEST_FILENAME} !-f
#AND if the file with php extension exists. Just an extra check
RewriteCond %{REQUEST_FILENAME}\.php -f
#AND if the request isn't for domain root
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1\.php?r=1 [L]
#redirect all urls ending with .php to "clean" URL
RewriteCond %{REQUEST_URI} ^/?(.*)\.php$
RewriteCond %{QUERY_STRING} !(^|&)r=1
RewriteRule ^(.*)\.php$ /$1? [L,R=301]
It will not make website.com/about.php look like website.com/about but it will display website.com/about.php if user visits website.com/about.

Syntax with .htaccess URL rewrite

I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/

Htaccess files and url rewriting

I have 2 questions.
I am currently using wamp server to serve my website.
The homepage is 'localhost/prefix/index.php'
Question 1:
a. I would like it so my home page is:
'localhost/prefix/'
instead of
'localhost/prefix/index.php
b. I would like it so:
'localhost/prefix/admin/profile.php'
is
'localhost/prefix/admin/profile'
How do I go about doing this (I have googled and I am very confused by the syntax)?
Question 2
If I have a url like
'localhost/prefix/games?title=hi'
how can I make it so the url is like this:
'localhost/prefix/games/hi'
Thanks in advance!!
I really have got lost.
EDITED::///
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R]
Is what I have so far.. It does nothing... But everyone says it should! (the htaccess file is doing something because if I do something random, it throws up errors).
EDITED::///
This seems to remove .php and index.php from the url:
RewriteEngine On
RewriteBase /prefix/
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Problem now is that my prefix base is not working so it ends up going to
localhost/something/something
rather than
localhost/prefix/something/something
Any ideas?
EDITED::///
I have sussed out that the above code actually works perfectly if the page i'm directing to is in a sub folder. so for example.. this will work:
localhost/prefix/admin/dashboard
But this (because the file is in the root directory, doesn't)
localhost/prefix/login.php
it redirects me to
localhost/login
Any ideas?
EDIT::///
If you are having problems getting it to work. close your browser down and restart... I had caching issues.
This code above will remove .php and also remove index.php.

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.)

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Resources