Mod_Rewrite with .htaccess is not working - .htaccess

I just learnt about url-rewrite for my website with .htacess. My actual url is:
localhost/index.php?view=some-page
So i write this RewriteRule like this:
RewriteRule ^/([^/]*)/?$ /index.php?view=$1 [NC,L]
When i typed localhost/homepage on my browser, It does not work, it displays error 404 object not found. What have i done wrong please show me.
Many thanks

This should work in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?view=$1 [QSA,L]
Leading slash is not matched in htaccess.

are you using apache?
This link from step 6 helped me
https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-14-04
When i was playing around with rewrites i had to enable it and tell apache where the pages were stored

Related

force index.php in url with htaccess

I've spent the last two hours reading stackoverflow and the apache manual trying to find a solution to this, but haven't found any, if a directory is access (http://domain.ext) I'd like to force index.php to appear in the url (http://domain.ext/index.php) if possible.
If anyone could help I'd really appreciate it, thank you.
RewriteEngine on
RewriteRule index.php
you can use this:
RewriteEngine on
RewriteRule ^$ /index.php?
(but you will not see it in a browser because the rewrite engine just directs to the correct file/directory without changing the url from the browser)
with [R] tag you can redirect the user so the new url would appear in the browser
I've just found what works for me
RewriteEngine On
RewriteRule ^/?$ $1/folder/index.php$2 [R=301,L]

What is wrong htaccess

I have the following htaccess rules but I dont know how to make it work:
RewriteEngine on
RewriteRule ^game/([^/]*)$ index.php?cash=$1
The following is the screen shot of my folders structure:
Can someone tell me how to get it to work? I have also tried:
RewriteRule ^index.php([^/]*)$ index.php?cash=$1
The URL that I want to display is: http://localhost/biteep/game/100 while the URL that I want the browser to go to is http://localhost/biteep/game?cash=100
Try to put a rewrite base into your .htaccess:
RewriteBase /biteep/
And this route should be enough:
RewriteRule ^game\/(\d+)$ index.php?cash=$1
So it works when I do this
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?cash=$1

Getting Error 500 when trying to use RewriteRule

I am trying to figure out the code to make all my links with no extension to be redirected to a certain page using .htaccess. For example:
http://mysite.com/link
Gets rewritten to:
http://mysite.com/run.php?id=link
I have tried the following code but I receive an Internal Error 500 and can't even access my homepage.
RewriteEngine On
RewriteRule ^([^/]*)$ /run.php?id=$1 [L]
Thanks for your help I think found the answer:
RewriteEngine on
RewriteBase /
RewriteRule ^.htaccess$ - [F]
RewriteRule ^([A-z,0-9,_,-]+)/?$ /run.php?id=$1 [QSA]
This seems to work. Although I'm not soo sure if anything else is affected. The homepage still loads if I type www.mysite.com/index.php so I think it is ok.

Using .htaccess and mod_rewrite

I'm having some mod_rewrite problems with my .htaccess..
RewriteBase /folder
RewriteEngine on
Rewrit­eRule ^(.*)$ basic.p­hp?­url=$1 [L]
Above is what I'm currently using. However, I have no idea what I'm doing to be honest as I'm just cycling through the internet trying to figure this out.
Basically, for my website, if you type in
www.domain.com/folder/xxx/
I want it to basically be www.domain.com/folder/basic.php?url=xxx.
For some reason, all that does is cause a 404 error :/
So can someone please politely point me in the right direction?
Ok, I will explain using your htaccess.
RewriteEngine on
Turn on the Rewrite Module to enable URL rewriting
RewriteBase /folder
Rewrite the Base Directory to directory name folder
RewriteRule ^folder/(.*)$ basic.php?url=$1 [L]
Remap every request make using the folder/***** to basic.php?url=********
Note: RewriteEngine On Should be the first statement on your case
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.*)$ folder/basic.php?url=$1 [L]
This is more of a problem with regexs than .htaccess files.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://www.regular-expressions.info/

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources