redirection with .htaccess (removing subfolder from URL and keep them into website) - .htaccess

I just want to redirect this kind of URL :
mydomain.fr/public/pages/project.php
To this kind of URL
mydomain.fr/project.php
I'm trying this kind of stuff in my .htaccess file
Redirect /public/pages/ http://mydomain.fr/
OR
RewriteEngine On
RewriteRule ^public/(.*)$ /$1 [L,NC,R=302]
(to remove just the 'public' folder).
Its working so when I click on a link of my website, the URL is redirected to what I want, but it broke the navigation. So I can't access my page (error like : The requested URL was not found on this server.) occured.
My question : How do I remove these subfolder in my URL without removing these subfolder into the server of my website ? I dont know if i'm clear, sorry my english is not perfect.
Thanks !
Have a good day

Related

Trying to rewrite a local url to another

I want to be able to do the following:
ReWrite /test -> /test.php
Also:
ReWrite /Test/test -> /Test/test.php
I want to be able to do this only for those mentioned urls. Also, I do not want direct access to .php urls. For example, if the user navigates to /Test/bla.php or any other links, he would be automatically be directed to the /index. The main reason I am doing this is that I do not want the user to know I am using .php. Here is what I tried:
RewriteEngine On
RewriteRule ^Test/test$ /Test/test.php
RewriteRule ^test$ test.php
ErrorDocument 404 /index.php
This works to some extent. I can navigate to /Test/test and it redirects me to /Test/test.php without issues. Though, if I do something like /test (from the root), I see the Test folder instead. Also, 404 does not work in this .htaccess above. I can navigate to any missing url and it complains that it is not there.
I am new to web hosting. So, I am kind of confused by the .htaccess.
Thanks for your time.

Redirect .asp pages to home page of my new site

I changed my website from .asp pages to Joomla which uses .php. The page structure has not been maintained. Now, I would like to have all traffic coming in from the indexed .asp pages (by the search engines) redirected to the home page of my site.
I have used the following Rewrite rule
RewriteRule \.asp$ ^/index.php [R=301,L]
but the rule redirects to
https://example.com/home/example/public_html/index.php?key=1234
How can I simply remove the /home/example/public_html/ and also the ?key=1234 parameter while performing the redirect. Or simply saying, how can I just have the redirect go to the home page of my new site.
Thanks.
First see this picture from Apache below :
So , a substitution could not contains Regular Expression as you did here ^/index.php this ^ should be removed first then see what you want to do .
https://httpd.apache.org/docs/2.4/rewrite/intro.html
Replace your code by this :
RewriteRule \.asp$ http://example.com? [R=301,L]
I put ? after example.com to prevent query string like key=1234 to be appended in new target .
Note: clear browser cache then test it .

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

Mod rewrite to redirect to 404 if filename is followed by other characters

On my website I have files index.php,home.php,about.php
And The full url is
http://example.com/about.php
and the wired thing is that
http://example.com/about.php#foobar
also goes to the about page . I want to block anything after my files name and redirect it to 404.
I copied this from a similar post :
Rewriterule ([a-zA-Z]+).([a-zA-Z]+) - [L]
Rewriterule ([a-zA-Z]+).([a-zA-Z]+) - [R=404,L]
Its not working. can you help me please?
Edit: its not a hashmark related question.
I want to redirect 404 a url with a trailing path.
I think you are looking for this :
AcceptPathInfo Off
Source : http://httpd.apache.org/docs/2.2/mod/core.html#AcceptPathInfo

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

Resources