RewiteRule not working in .htaccess - apache

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)

Related

Apache Server redirection issue on .htaccess

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

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>

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 /

How can I remove "index.php" from my links?

Normally I access a page like this: http://localhost/codeigniter/index.php/auth/login
However I want to be able to access to the page like this : http://localhost/codeigniter/auth/login
I have no information about http.conf and .htaccess configurations.
I have set $config['index_page'] = ''; in codeigniter/application/config/config.php
my http.conf file is at its default except uncommentnig "LoadModule rewrite_module modules/mod_rewrite.so"
I inspected many proposed .htaccess files but I could not understand the logic behind the rewriting rules.
Here is the most successful .htaccess content for me. It is located at /www/CodeIgniter/ .
(by the most successful I mean that this is the only one that gives 404 error created by the CodeIgniter not by the server.)
RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
I hate requesting direct solutions however unfortunately, it is my last resort :/
Can you give me a right solution to my problem?
It's my repeat answer:
Codeigniter issues with paths in localhost (XAMPP)
please create .htaccess file in project folder and write:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
You don't need to define in base_url in config file:
$config['base_url'] = ''; // blank it.
put this on your htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
make sure the rewrite module is enabled on your server
This is the simplest one i found which works for me :-
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Apache: Can't reroute URL from "file.123.ext" to "file.ext" using mod_rewrite in htaccess file

I'm trying to implement cache busting, as prescribed by: http://html5boilerplate.com/docs/cachebusting/
However when I add the following to the .htaccess file in my media folder:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
and then I try to access a style sheet at: "/media/css/styles.1234.css", Apache complains with:
Not Found
The requested URL /media/css/styles.1234.css was not found on this server.
I can access styles.css at "/media/css/styles.css" so it's there and it works. I also know that the htaccess file gets processed because we also disallow directories from being browsed in the media folder with "Options -Indexes". Here is the htaccess file in its entirety:
Options -Indexes
AllowOverride All
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
Also mod_rewrite has been confirmed to be loaded with this command: 'a2enmod rewrite'. Not sure what I'm doing wrong...
Here is the solution to my problem... Even though mod_rewrite was loaded, it needed to be set to on. So I just aded "RewriteEngine On"... resulting htaccess file:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
Relative path rewrite, missing RewriteBase: fail!
I had the same error, improperly used Header set X-Frame-Options "DENY". Quick fix might be to temporarily comment this out. Hope that help someone who need effective solution 'for now'.