Mod Rewrite and Include issue - php

I've googled for some mod_rewrite tutorials, but couldn't figure out how to solve my specific problem regarding the way I created my site with includes.
I've an index.php and in this file there are several includes like index.php?s=events will include events.php and so on.
If I use this code:
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)$ index.php?s=$1 [L]
it will result that www.mydomain.com/events just shows the events.php itself, not with the "skeleton/framework" index.php around it, like it should be. So it just loads the specific included file.
My target is to call www.mydomain.com/events for example to show the whole index.php?s=events page.

Try putting slash before URL into the RewriteRule:
RewriteEngine on
RewriteRule ^/?([a-z_]+)$ /index.php?s=$1 [L,NC]
I added NoCase flag to get rid of capital letters in the regexp.

Related

.htaccess on 2 inputs

Right now I have the following: (this is all that is in the .htaccess file under the /page/ dir)
RewriteRule ^(.*)$ set.php?numbers=$1 [QSA,L]
This correctly pulls with the following URL:
http://domain.com/location/page/XXX/
However, when the page loads I have the following links I want to click:
http://domain.com/location/page/set.php?numbers=XXX&otherset=morenumbers
Issue:
The other link does not seem to work because the HTACCESS rules are breaking the query strings. Is there a way around this so that the links work and that htaccess does not break it?

mod_rewrite issues and php

I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??
Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links

Problems with Apache mod_rewrite

I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />

Using a variable in the address bar to display an image?

I have a little problem I can't seem to get my head around. What I'm trying to do is that when someone "http://domain.com/directory/ImgVariableHere" it will display the image on a php page. I know I'm probably going about this the complete wrong way and that's why I've come here. I'm a newbie to PHP so please forgive me. Here's the code I have.
<?php
$img="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo "<img src=\"$img.png\">\n";
?>
Here's the code it echos.
<img src="http://domain.com/directory/.png">
I'm probably explaining this really badly, ask any questions of you need to. Thanks. If you have a completely different and better way of doing it then please do tell.
EDIT: What I'm trying to do is kind of like Gyazo. They show their images on a webpage, I'm trying to do that but by using variables in PHP and I just can't wrap my head around it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Put this in .htaccess in the root of your document, and it should route all requests to index.php, then you just put the script above in index.php, and it should work.
It looks like you want to have a RewriteURL with a variable in it.
So when you want this we can create a .htaccess which will reserve a spot for your variable. Example is like this:
RewriteEngine on
RewriteBase / #Maybe your in a subfolder? Change this to /yoursubfolder/secondsub/...
#Redirect all urls that begin with /image/ to image_shower.php?image=<image value>
RewriteRule image/(.*) image_shower.php?image=$1
This rewrite all the urls that begin with /image/ to the image_shower.php without showing the image_shower.php URL. It also sends the information in a GET variable. Which can be easily retrieve in PHP. Like this:
<?php
$img="http://".$_SERVER['HTTP_HOST']. '/' . $_GET['image'];
echo "<img src=\"$img.png\">\n";
?>
Hope this will help you go further!

mod_rewrite RewriteRule and html links

I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?
Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>
Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [L]

Categories