Apache Server redirection issue on .htaccess - apache

I have written HTML page and want to set RewriteRule. I have created .htaccess file into the folder written these:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ test.html [QSA,L]
It just redirects to http://example.com/test.html . I want to redirection to http://localhost/project/test
I think it is caused by httpd.conf or hosts file. But I can't find a way.
Edit: .htaccess file is under C:\xampp\htdocs\project
Thank you.

You may use this rule with appropriate RewriteBase value:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ test.html [L]

I dont understand why do you want to use .htaccess file for redirect. Also note You can not move your site to localhost. You may directly do from your file
String site="/abc.com/test.html"
Response.setStatus(response.SC_MOVED_TEMPORARILY)
Response.setHeader("Location", site);

Related

RewiteRule not working in .htaccess

I'm making a website in PHP and I've got to the part where I need to make a news page.
I have already written the news.php file and it uses GET to read the particular title of the article to display. That means I end up with an URL like
/community/news.php?title=post-title
And I much rather like it to be like
/community/news/post-title
So I looked it up and saw I could do it with the .htaccess file.
Now, my actual .htaccess file looks exactly like this
ErrorDocument 404 /notfound.php
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^community/news/([^/]*)$ /community/news.php?title=$1 [L]
</IfModule>
And the result of going to /community/news/post-title it's a 404 Error
Thank you so much for your help.
If you work in local environment and your url is like http://localhost/website/community/news/post-title the you should have "website" in "RewriteBase /"
RewriteBase /website
So, assuming your url is example.com - (the QSA flag appends the query string string to the url)
RewriteEngine On
ErrorDocument 404 example.com/notfound.php
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^community/news/(.*)$ example.com/community/news.php?title=$1 [L,QSA]
Thanks a lot for your help.
Turns out it was an issue with relative paths.
It worked with:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/community/news/([^/]*)$ community/news.php?title=$1 [L]
</IfModule>
(the difference is in the '/'s of the last line)

How make htaccess for single-page app within a sub-path in URL?

I want to have a single-page app, but only within a sub URL like:
//The following would all be valid, but handled by the "index.html"
//at http://www.test.com/webapp
http://www.test.com/webapp
http://www.test.com/webapp/settings
http://www.test.com/webapp/start
I want all the URLs to look exactly like above, but be handled by: http://www.test.com/webapp/index.html.
I also want the following URLs to not be handled by that page:
http://www.test.com
http://www.test.com/contact
http://www.test.com/about
How do I do this?
REGULAR SINGLE_PAGE HTACCESS
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QA,L]
</ifModule>
Just adding answer for reference.
The commnet from #Croises:
"The same (with [QSA] not [QA]) but in the webapp directory"
Worked for me.
The ".htaccess" must be on the subdieretory.
Example working ".htaccess":
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [QSA,L]
</ifModule>
The only diference is "[QA,L]" to "[QSA,L]"
The answer marked as good didn't work for me. Below my solution:
1) Create .htaccess in the folder where you place your app
2) Paste the code below
3) Change folder_name to your folder name.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder_name/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder_name/index.html [L]
</IfModule>

Rewriting ends up with not loading js / css

With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!

Apache Rewrite custom rules file

I have a server set up hosting a website. It uses "pretty links". I am having a problem when trying to create rules in the htaccess file.
Before I continue I would like to reference the locations I use to minimise confusion
wwww/ the wwww is the site all my folders content in and it resides in the www folder created by wamp
page.php contains the rules I want to somehow impliment and this is located in the root of my website
currently my website produces links like the following which dont work:
www.MYDOMAIN.co.uk/our-services
the link above does not work but if I manually edit the link and do this it works:
www.MYDOMAIN.co.uk/page.php?q=our-services
I need to find a way to implement this in the htaccess file and get it to add the page.php?q=
Below is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . wwww/index.php [L]
</IfModule>
The problem I am having is on my home page the navigation is generated and if I click a link for our services it redirects to the page www.MYDOMAIN.co.uk/our-services
What I need it to do is to hit my rule file which is in my root directory called page.php which in turn will use the our-services or whatever the query might be to give it the correct link
I know my htaccess file is currently linking back to the index file and this is why it isn't working but I have tried other methods as seen below.
ATTEMPT 1 - this just 404's or takes me to a blank page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . wwww/page.php?q=$1 [L]
</IfModule>
ATTEMPT 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /page.php?$1 [L]
</IfModule>
My other attempts are all similar and I get the same 404 or blank screen. This is my first time getting into Rewriting links which explains my lack of knowledge about it.
Ok try this rule from /wwww/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wwww/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ page.php?q=$1 [L,QSA]
</IfModule>
If /wwww/ is indeed your DocumentRoot then change RewriteBase line to:
RewriteBase /

Apache ignore htaccess

I have a website that uses mod_rewrite for pretty urls. I have two main urls:
example.com/id
example.com/generate/id
And this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^generate/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]
RewriteRule ^([a-z0-9_-]+)$ contact.php?id=$1 [L,NC]
The first url is working correctly, but the second not. Apache shows a 404 error, I think that it's because it's looking for the folder "generate", that doesn't exist, and it can't find the htaccess in the document root.
Thank you
Have you try
RewriteRule ^generate\/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]