htaccess RewriteRule not working - (404 error) from server - apache

I'm trying to rewrite my URL from this:
http://www.example.com/admin/index.php?id=title
to:
http://www.example.com/admin/title
I'm using this code in my htaccess:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /admin/index.php?id=$1 [L]
But then when I try out rewritten URL's i get a 404 error from my server. What is the mistake I'm making? The .htaccess is in a subfolder called admin and the rewrite rule should only work for that folder.

This .htaccess should be placed inside the folder admin that must be inside your root folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?id=$1 [L]
The 2 conditions make sure we are not redirecting an existent file or folder and rule tells we want to extract anything not a / and use as the ID.
The RewriteBase tell us your parent folder is admin and as such we work from there and onwards.

Related

rewrite problems (htaccess)

Having some experience with procedural php I watched some tutorials about OOP and the MVC model (with php). Things start to get more clear and I wanted to put the theory to practice.
The tutorial I'm following works with an app folder and a public folder, both subfolders of the root directory. There's an index.php file in the public folder and a htaccess file that redirects all requests (in the public folder) to none existing files to index php. The code in that file is:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
That works fine.
In the root folder there is also a htaccess file with the purpose of redirecting all url requests to the public folder (in case /public/ is not in the url. The code in that file is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
That goes wrong, and it's the second RewriteRule that causes the problems. If I for example browse to
www.mywebsite.com
the browser redirects to www.mywebsite.com/public/index.php
But if I browse to www.mywebssite.com/shop (shop is not an existing file) I suppose the browser redirects to www.mywebsite.com/public/index.php?url='shop', but instead there is an internal server error. It seems to be the second RewriteRule that causes the problem.
What could be the problem?
I am on mobile I haven't tested it but looks like you could be reaching out to maximum redirect limits here why because your condition in your root htaccess isn't looking good to me, try this once.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^/public/? [NC]
RewriteRule (.*) public/$1 [L]
</IfModule>
Issues in OP's approach: You haven't mentioned any condition to when it should redirect so it doesn't know when to stop hence it's creating a loop here IMHO.

.htaccess redirecting sub domains to sub domains name folder

My site structure is as follows:
/
static/
www/
index.html
img/
www2/
index.html
img/
www3/
index.html
img/
I have this .htaccess that does the following:
According to a subdomain for instance www.domain.com/index.html i redirect to the right subdomain folder which is /www/index.html.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</IfModule>
It works perfectly when i am accessing the site with www.domain.com/index.html
but not without index.html meaning
www.domain.com/ .
I have tried many condition but non of them seems to work , and if it works it breaks what i have now.
Which condition should i add for redirecting the www.domain.com/ to /www/index.html ?
Thanks
EDIT
The error i am receiving is:
"Forbidden You don't have permission to access / on this server."
meaning i am redirected to the / folder which doesnt contains an index.html.
To me it sounds like you don't have a directory index set. There is nothing wrong with going directly to the file but apache doesn't consider index.html your index file. And with directory listing turned off you will get that error. You don't need rewrite for that. Use the directive. That should prevent the 403 Forbidden.
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</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 /

Changing url with .htaccess

I have a site like "abc.com/1_en-Application.html . Now i want to change it to abc.com/application.html.
I have .htaccess like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9]+)-([0-9]+)_(.*)-(.*).html$ index.php?id=$1&page=$2&lang=$3 [L]
RewriteRule ^([0-9]+)_(.*)-(.*).html$ index.php?id=$1&lang=$2 [L]
</IfModule>
Please help me how can i do this ?
Add the 3 lines below the existing rewrite rules:
RewriteCond %{REQUEST_URI} /application.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^application.html$ index.php?id=1&lang=en [L]
If you don't have a real file called application.html in the web root directory, this will rewrite the URL and internally direct it to index.php
If application.html is a real file, it will be shown instead. In that case, the 3 lines will still work but not necessary to include.

URL Rewrite : Pointing wildcard url to its subfolder

I need to show content of wildcard url to its particular subfolder ..
For Example
Right now http://youbridge.info/watchvideosonline/random-hello.html is showing content of home page , instead i need to show contents of that particular subfolder/index.php (i.e content of http://youbridge.info/watchvideosonline/)
I tried below code , but its not working
RewriteRule ^([^/]+)/?$ index.php?filter=$1 [QSA,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/.*$ /$1/index.php [L]