How do I hide a /public/ directory in htacess - .htaccess

My website is website.com/public/login.php. That looks a bit ugly. How do you rewrite htaccess to say website.com/login.php?
I've tried
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

The RewriteRule that strips off the /public/ path is currently an external redirect, because of the R=. Since you want the result of that rule to be hidden, it should rather be an internal redirect (also called URL forwarding), and that is written without the R=302 option.
While you're at it, you could also hide the .php from the externally visible URLs since a simple /login looks much cleaner than /login.php.

Solved. It hides /public/
website.com/login.php instead of
website.com/public/login.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Related

create a rewrite condition for following URL

i have following URL
http://localhost/dyescan/pair_pixel_filter?type=0
i want rewrite Rule and write condition for it, so far i have done this
<ifModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /dyescan/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?type=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php [L]
</ifModule>
what happens here is that i get my desired page but, i now cannot access my CSS libraries also if there is another xpage some thing like this
localhost/dyescan/sompage.php?test=1
it redirects me to the appache default page. what is the workaround for this ?
Change:
RewriteRule ^ /%1/%2? [R=301,L]
to
RewriteRule ^ /%1/%2.html? [R=301,L]

.htaccess, Redirect Non-Existing Except a Few URL Rewrites

I have a webpage where if non-existing files or folders were to be typed by a user http://wwww.somewebsite.com/nonexistingfolder or http://www.somewebsite.com/nonexistingfile.html will just simply forward to www.somewebsite.com. Then would like to create a web page from www.somewebsite.com/about.html to www.somewebsite.com/about since shorter is better in my opinion. Using .htaccess I thought I could use RewriteCond for non-existings and RewriteRule for the user-friendly like web page url. I'm terrible at .htaccess I only know basics, I did my research even the questions that may have been asked already but not sure how to write this exception rule.
How can I add a code in .htaccess so that I can have all non-existing files/folders except the web page url I specified?. This one below will simply redirect all non-existings to index.html and when I do www.somewebsite.com/about (from /about.html) simply goes to index.html. Any help?
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
You need to do the all-or-nothing rewrite (e.g. the RewriteRule ^(.*)$ / [L,QSA]) after all of your other more specific rules. The rules all get applied in the order that they appear in, so this means your first rule simply always gets applied, and then the rewrite engine stops.
Swap the order around and try again:
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/?$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
Also this rule:
RewriteRule ^/$ index.html [L]
Will never get applied because leading slashes are removed from the URI before applying rules to them, so ^/$ will never match, you want ^$ or ^/?$ instead.

.htaccess rewrite to subfolder with codeigniter rewrite

I have a site that needs to exist in a subfolder
example.com/site
But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
This works well when there is a page identifier specified, so
www.example.com/site/book rewrites to example.com/site/book
But when there is no page identifier specified I get a 404
www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new
I was wondering if anybody could point me in the right direction?
This is my full .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You may try this instead:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.*) [NC]
RewriteRule .* http://example.com/%1 [R=301,L]
Maybe, you're just missing a RewriteBase
Depending on where the .htaccess file is, try either
RewriteBase /
or
RewriteBase /site
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

Rewrite static html to dynamic php error bad flag delimiters

Having gone through some suggestions about rewriting static url to php url i have hit snag trying to redirect http://www.example.com/clothing to http://www.example.com/index.php?main_page=index&cPath=999
using this rule
RewriteRule ^clothing/index.html index.php?main_page=index&cPath=999 [L]
I keep getting
rewriterule: bad flag delimiter
even after changing/adding / and \ to the rule.
This is my .htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# From Ultimate SEO URLs
RewriteRule ^(.*)-p-(.*).html$ index\.php?main_page=product_info&products_id=&% {QUERY_STRING} [L]
RewriteRule ^(.*)-c-(.*).html$ index\.php?main_page=index&cPath=&%{QUERY_STRING} [L]
RewriteRule ^(.*)-m-([0-9]+).html$ index\.php?main_page=index&manufacturers_id=&%{QUERY_STRING} [L]
RewriteRule ^(.*)-pi-([0-9]+).html$ index\.php?main_page=popup_image&pID=&%{QUERY_STRING} [L]
RewriteRule ^(.*)-pr-([0-9]+).html$ index\.php?main_page=product_reviews& products_id=&%{QUERY_STRING} [L]
RewriteRule ^(.*)-pri-([0-9]+).html$ index\.php?main_page=product_reviews_info&products_id=&%{QUERY_STRING} [L]
RewriteRule ^(.*)-ezp-([0-9]+).html$ index\.php?main_page=page&id=&%{QUERY_STRING} [L]
# All other pages
# Don't rewrite real files or directories
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ index\.php?main_page=&%{QUERY_STRING} [L]

htaccess 301 redirect example.com/beta/xyz to example.com/xyz

I need a general rule that maps every URL in the /beta subdirectory to the corresponding URL in the root; essentially I need to remove /beta from all URLs.
In case it makes any difference, the URLs are dynamically generated by WordPress.
Currently my .htaccess file is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END WordPress
Can you tell me where to put the new lines?
Thank you!
Could you try this?
RewriteEngine On
RewriteRule ^beta/(.*)$ http://example.com/$1 [R=301,L]
Your question is not entirely clear, but I would bet that what you want is having a Wordpress installed in somedir/beta appear in yoursite.com/ instead of yoursite.com/beta/. The rules you pasted are in somedir/beta/.htaccess, and are the default Wordpress rules. You must leave those alone.
What you need for that is to put the following rules in the root directory, as in, somedir/.htaccess, after changing example.com to your own domain. Your webserver first reads this root .htaccess, and when it does, it will know to rewrite requests to /beta.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/beta/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /beta/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ beta/index.php [L]
</IfModule>
More info in the Codex:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

Resources