.htaccess RewriteRule is acting strange - .htaccess

I have a webpage, which url looks like this:
http://www.mydomain.com/folder/index.php?title=The_title
The only problem is that I want it to look like one of these:
http://www.mydomain.com/folder/The_title
http://www.mydomain.com/folder/The_title.php
I have searched most of a lot and found one solution, which "works":
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /folder/index.php?title=$1 [L]
It does what it should, except the ending has to be ".html" (or ".kl", if you like). This solution won't accept ".php", which gives me a "500 Internal Server Error". Same thing happens if I try it without the extension.
EDIT:
Forgot to mention that the .htaccess file lies in the folder and not the root.

This will work for you.
RewriteEngine On
RewriteRule ^folder/([^/]*)\.php$ /folder/index.php?title=$1 [L]

So, since I couldn't get it to work properly I took the challange and started reading about mod_rewrite, which led to the solution:
RewriteRule ^(\w+)/?$ index.php?title=$1 [L]
If anyone else run into troubles I'll recommend these sites:
more .htaccess tips and tricks..
An In Depth Guide to mod_rewrite for Apache

Related

mod_rewrite RewriteRule in .htaccess is not working

I'm facing a strange issue with mod_rewrite RewriteRule in .htaccess.
Here's my minimal .htaccess file:
RewriteEngine On
RewriteRule ^phpinfo phpinfo.php [L]
The above Rewrite results in 404 Not Found. However, suffixing anything to ^phpinfo works which is unintended and undesired.
RewriteEngine On
RewriteRule ^phpinfophp phpinfo.php [L]
Please help me with making the actual RewriteRule work.
Thanks,
Shyam Singh
RewriteRule ^phpinfo phpinfo.php [L]
MultiViews often causes problems in instances likes this, because its job is to detected slightly misspelled URLs, multiple possible file matches (different extensions, etc.) and correct them – and it interferes here, because it sees a request for phpinfo and finds a corresponding file phpinfo.php
Deactivate it using Options -MultiViews, that should fix the problem.

Mod_Rewrite with .htaccess is not working

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

URL Rewriting using mod_rewrite

i'm trying lear to make a simple url rewrite and can't understand why is not working.
I created the file .htaccess with this:
RewriteEngine On
RewriteRule ^section\.php$ /how-does-it-work/ [R]
All i want to do is /section.php be displayed as /how-does-it-work/
i'm following a tutorial but doesn't seem to work.
Nothing seems to work… i even tried a mod_rewrite generator, i'm using GoDaddy, is there anything i should activate or something? 'cause so far i'm only dealing with the .htaccess file.
Thanks
You are using them the wrong way around. Try:
RewriteRule ^how-does-it-work/?$ /section.php [L]

Rewriting URL with .htaccess local in XAMPP

My .htacces begins with
RewriteEngine on
RewriteBase /
(I tried it also without RewriteBase...)
I tried all of the following rewriting rules to rewrite the URL
index.php?page=news
to
/blog
RewriteRule ^/?([-A-Za-z0-9]+)/([-A-Za-z0-9]+)/blog$
index.php?page=$1 [L]
RewriteRule ^([^/]*)/blog$ /sites/blog/index.php?page=$1 [L]
RewriteRule ([a-zA-z]+)/([a-zA-z]+)/blog$ index.php?page=$1 [L]
Nothing works - no error. Mod_rewrite is installed and working. I restarted Apache and MySQL everytime I changed something in my .htaccess.
I also want to change my URLs which looks like this... index.php?page=single_news&category=release&id=9&headline=Beastie%20Boys%20III
...into: blog/release/9-Beastie-Boys-III
I am lost. Hope you can help me.
First of all, upload your .htaccess and other files (whole project) to some working, ready hosting server. And check, if your rewriting works OK there. This will let you know, if this is problem with .htaccess or XAMPP itself. I had many strange problems with using .htaccess locally, under XAMPP, that were magically gone, after files were uploaded to Internet hosting.
For example, I don't have working autorization using .htaccess locally, because right after I provide correct login and password I see exactly the same error message as you mentioned. As for me, I'm more than sure that this problem is purely related to incorrect interpretation of .htaccess done by XAMPP (as everything works like a charm on production server), not by some mistakes in .htaccess contents.
I wasted (too) many hours on finding solution and left it. For right now, if I'm developing locally, I rename ".htaccess" to "htaccess", so it is ignored by XAMPP (Apache on-board of it) and re-enable it only when deploing files to production server. This approach maybe isn't to professional, but it saved me a lot of time and stress! :]
On the other hand, if your hosting also fail with the same symptoms, then you'll know, that this is not XAMPP releated problem and you have something wrong with your syntax.
Take a look here for a similar problem reported on StackOverflow.com, where (as I think) the cause is the same as in your issue.
Here's the solution to change links from http://www.domain.tld/index.php?page=blog to http://www.domain.tld/blog is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]
and for links like: http://www.domain.tld/index.php?page=single_news&id=1&headline=This%20Is%20A%Headline
the solution is:
RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2
After using this code, links looks like this: http://www.domain.tld/blog/2-this-is-a-headline
For your first question, try this:
RewriteEngine on
RewriteRule ^/blog$ /index.php?page=news

get the index.php with .htaccess

I'm using a bunch of ReWrite rules in my .htaccess such as these
RewriteRule ^brochure/([^/]+)/?$ brochure.php?cat_path=$1
It's all working great thanks to answers I found by searching this site. But I've got a tiny little snag.
This rule works perfecty at the moment
RewriteRule ^([a-zA-Z0-9_-]+)?$ $1.php It catches all my urls like www.mysite.com/shane and requestes shane.php - So far so good, but when I call www.mysite.com I need it to request index.php
Which rule could I use for this?
Thanks people.
UPDATE - For the sake of future users needing similar help.
My rule of ^([a-zA-Z0-9_-]+)?$ $1.php was causing the problem I mentioned above. The question mark ? is not needed in this type of rule, when you remove the question mark the index.php file in the directory returns to being the default landing page. Thanks to Tomalak for the Help.
You don't need any rule for it. Just make sure that .php default index files for the directory and it'll happen automatically.
Update
I think that your rule
^([a-zA-Z0-9_-]+)?$ $1.php
should in fact be
^([a-zA-Z0-9_-]+)$ $1.php
so that it does not conflict with requests where the filename isn't given (which you want to direct to index.php).
I think this is what you're looking for,not sure thought:
RewriteRule ^/$ index.php
You can either do as Tomalak says, and ensure the DirectoryIndex is set to load index.php.
Or close to what James says,
RewriteRule ^/?$ index.php [L]

Resources