Redirect to a subfolder using htaccess - php

I have searched all the similar topics in the forum and nothing helped me. Here is what I am looking for:
I have a site having a login url as
site.com/folder1/php/login.php
Now I want to hide the "php" folder name from url, also remove .php extension so that it will look like
site.com/folder1/login
Also the folder name "folder1" may vary, but the folder name "php" remains the same in all urls.

Try this in your Root/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/?([^/]+)/([^/]+)/([^.]+)\.php [NC]
RewriteRule ^ /%1/%3 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /folder1/php/$2.php [NC,L]

This should be working.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+php/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^php/)^(.*)$ /php/$1 [L,NC]

Related

htaccess remove directory from full URL with string

I want htaccess to remove directory (/thedir/) from full URL but it's not working. I only want to remove the /thedir/ from this URL not other urls.
redirect 302 /thedir/?s_id=ff7d7ds7&bmt_source=facebook&bmt_camp=octy-slant&bmt_medium=ball https://example.com/?s_id=ff7d7ds7&bmt_source=facebook&bmt_camp=octy-slant&bmt_medium=ball
Also tried
RewriteCond %{QUERY_STRING} ^/thedir/?s_id=ff7d7ds7&bmt_source=facebook&bmt_camp=octy-slant&bmt_medium=ball
RewriteRule ^(.*)$ https://example.com/?s_id=ff7d7ds7&bmt_source=facebook&bmt_camp=octy-slant&bmt_medium=ball [R=302,L]
These examples do not remove /thedir/.
Try these rules in your .htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+thedir/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^thedir/)^(.*)$ /thedir/$1 [L,NC]

Remove .php from URL - Specific case

This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

.htaccess rewriterule folder and files rewrite

I have problem with my rewrite rule. I have new webpage in root/hrp. and if i open pages in that directory, then all is OK, but when i wanna surf those pages from root (without /hrp/) then is problem. I found some .htaccess rules and all working fine, but if i wanna execute some pdf,jpg,or php script directly, then come problem.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule ^(.*)$ /index.php [L]
if i put this bottom code on begining, i can open files directli, but pages don't work and vice versa
RewriteCond %{REQUEST_FILENAME} (/|\.php|\.html|\.htm|\.png|\.jpg|\.jpeg|\.gif|\.xml|\.rss|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule ^(.+)$ $1 [L]
Try adding this at the top instead:
RewriteRule \.(php|html?|png|jpe?g|gif|xml|rss|feed|pdf|raw)$ - [L,NC]
or you can try adding this condition to your hrp rule:
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -d
RewriteRule (.*) /hrp/$1 [L]
I have find solution
don't rewrite this files
RewriteCond %{REQUEST_URI} !.(gif|jpg|png|ico|css|js|swf|wav|mp3|less|cur|pdf|jpeg|txt|ico|zip|gz) [NC]
don't rewrite this folder - there i have php files, and dont rewrite this files
RewriteCond %{REQUEST_URI} !(folder1|folder2|file1.php|file2.php|file3.php) [NC]
all content is in "/hrp/" folder
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
but url don't contains "/hrp/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule (.*) /index.php [L]

Good profile urls using .htaccess

I've three url types which are as follows:
http://www.mywebsite.com/myprofile.php?user_username=username
http://www.mywebsite.com/input.php?user_username=username
http://www.mywebsite.com/users.php?user_username=username
Currently, users have to type the whole address like (http://www.mywebsite.com/myprofile.php?user_username=username) to go to their profiles and the same with input and users.
What I want is if a user types http://www.mywebsite.com/profile/username, he would be automatically redirected to http://www.mywebsite.com/myprofile.php?user_username=username.
When a user types http://www.mywebsite.com/input/username, he would be redirected to
http://www.mywebsite.com/input.php?user_username=username.
When a user types http://www.mywebsite.com/username, he would be redirected to http://www.mywebsite.com/users.php?user_username=username.
I know that this can only be achieved through .htaccess. However, I've searched but with no fruitful result.
Any help please!
Update
I think that the following code is close to being correct. However, I'm not getting the correct result and the css is also getting messed a lot. Individually they are working but together they are messing up. Any help please.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /myprofile\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /myprofile/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myprofile.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /user\.php\?user_username=(.*)\ HTTP
RewriteRule ^ //%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /input\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /input/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ input.php?user_username=$1 [L]
Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+input\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /input/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+myprofile\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+user\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [NC,QSA,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [NC,QSA,L]
Add these to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [DPI,L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [DPI,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [DPI,L]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

CodeIgniter: How to properly setup htaccess for domain forwarding to a subdirectory of a subdomain?

I have CodeIgniter setup in a sub-directory of a sub-domain. My htaccess file works for the following: http://subdomain.domain.com/subdirectory/
The working htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
However this does not work when I point a domain name http://www.mydomainname.org to public_html/subdirectory.
What I'm attempting to achieve is I'd like to use http://www.mydomainname.org/whatever/ to mask http://sub.domain.com/subdirectory/index.php/whatever
Is this possible? Is there anything I can add to my htaccess that will allow me to achieve this?
I attempted to use the htaccess answer here, but had no luck.
I think if you want both to work, you'll need to add extra conditions for the hostname, try something like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteRule ^$ /index.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php?/$1 [L]
# for subdomain
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^$ /subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(?:subdirectory/|)(.*)$ /subdirectory/index.php?/$1 [L]

Categories