Rewriting URL with .htaccess local in XAMPP - .htaccess

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

Related

"Resolve to the same URL" and .htaccess file for HTML built website

A while ago I made my first website using only HTMl code and some basic inline CSS.
I don't have the experience and the intuitive knowledge of how things work in this world that is why even the most basic errors I have to solve or tweaks I have to make gives me headache.
My website looks like it's duplicated because when you enter the www or the non www version, then both work and for the search engines it looks like there are two identical websites, which is not good for me.
One tool showed me an error saying: "https://domain.lv and https://www.domain.lv should resolve to the same URL, but currently do not."
I've tried different methods to solve this by editing my .htaccess file, but nothing works. I'm sure there is some dumb mistake I am making because I don't know how should even basic things look like when they are right.
This here is my .htaccess file now (for this I used the word domain as a placeholder for my real domain name):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.lv [NC]
RewriteRule ^(.*)$ https://domain.lv/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
ErrorDocument 404 /404.html
Do you see what is wrong here and how I get it right?
And maybe someone could give me a sample file of how .htaccess file should look like for websites built from scratch?
I will appreciate any help.
Cheers!

Rewrite rule behaving differently between environments

I have two environments, both using Bitnami installations (if that makes a difference). My local environment is WAMP and works correctly. The second is on EC2 using LAMP and does not work correctly. I've done some basic checking and things seem to be setup the same but still give different results.
RewriteEngine On
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
The .htaccess page is the same, as are the folder configurations. I have all files in the root folder.
-root
-index.php
-study.php
If I go to //localhost/study/ it correctly redirects to index.php. However on my EC2 instance, it instead goes to study.php. Even if I go to //ec2/study/random/2021/bleh/ it will redirect to study.php. Going to //ec2/random/ will however go correctly to index.php since I do not have a "random" folder.
I've run out of ideas of where to look for configurations differences (I even set "AllowOverride All" in the httpd.conf file and tried -MultiViews). Any help/ideas would be greatly appreciated!

.htaccess RewriteRule is acting strange

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

.htaccess - Localhost working in a folder problems

I'm working localhost on my mac with MAMP. And I'm trying to do some mod_rewrite with .htaccess. I think all my problems is about that I'm working on a URL like this: localhost:8888/folder/index.php.
How do I either "remove" the /folder/... Or make .htaccess work with /folder/ having in mind that I wanna remove it as soon as my project goes live? *I would prefer to "remove" the /folder/ to make it look more like when my project goes live.
If I am trying to for example "remove" the index.php like this:
#Removes index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
It's jumping all the way back to root: localhost:8888/ instead of localhost:8888/social/.
Had the same problem and just found a solution.(I know i may be late to this post, but might help some else)
The problem I had was with the my local web enviorment. I tried it on the server and everything was working! So found out that I just needed to set up the Virtual Hosts for my sites in apache and everything worked great!
I did this with the help of this stackoverlfow post: How to set root directory to a subfolder and this article on how to do this on a windows PC: Setting up Virtual hosts in win 7
Hope it helps,
Cheers!

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