Rewrite URL with parameters passed through PHP - php

So I have my PHP page page.php with the parameter id for example.
Everything works as expected when I visit
page.php?id=1
What I try to do now basically is, reach the site when the user visits
page/1
I've looked at countless questions and websites about htaccess rewrites and so on, but I'm not even entirely sure if I need that. I did the exact same thing in an old project of mine where I can't find any htaccess file, but I also have no idea what I really did.
And in case I do need the htaccess file. How do I ensure that it works properly?

Alright, gotta put this in the htaccess
RewriteEngine On
RewriteRule ^page/([^/]*)$ /page.php?id=$1 [QSA,L]

Related

server error using URI embedded images in a required file if index.php isn't in the url

Struggling. Help!
In index.php:
require "cachedPages/home.html";
If I visit: https://websiteaddress.org/index.php then it works fine.
If I visit: https://websiteaddress.org then I get an internal server error.
I guess it's a .htaccess thing. All I have in there is some cpanel php72 code and:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
It seems there's some difference between how index.php is called if you call it directly as opposed to .htaccess calling it for you?!?
Any ideas?
Thanks in advance.
If you don't have one of the following options in your .htaccess then Apache won't know which file to default back to:
DirectoryIndex index.php
FallBackResource /index.php
Also I'd recommend installing mod_rewrite as well. It's handy for other reasons.
Well, I've managed to sort the problem, but it's a pretty weird situation that no-one else will probably ever experience. Here goes...
If the user lands at:
https://websiteaddress.org rather than https://websiteaddress.org/index.php
and that page (php) requires another page,
which has images that are embedded as URIs rather than linked src files.
Then, the first URI causes a server error.
If I replace the
img src='data:image/jpeg;base64,/998a9g98ahg...etc'
with
img src='path/to/file.jpg'
on the first instance of a jpg then it all works fine.
All the later URIs are fine, it's just the first instance!
It all works now, with this workaround; and the situation is so unique and bizarre that I doubt this thread will be of use to anyone else. In fact it's so edge-case that I can't be bothered investigating it any further myself.

.HTACCESS RewriteRule not working as expected

I have a magento website, that is currently operating. Within a subfolder of this site, I have placed a 3rd party application that also has its own HTACCESS file to handle routing for its application.
When I access the folder http://example.com/somefolder the screen I expect shows up, but when I navigate to http://example.com/somefolder/newroute, I instead land on a magento 404 screen.
I have traced this to the magento htaccess file, in all cases, unlesss the path physically exists the rewriterule will always send the request to the index.php - this explains why Im getting there.
To fix this issue, I wrote a little rewriterule which I placed in the htaccess file of the magento store. The goal was to add an exception to any request that came through and contained any reference to my subfolder. The thought is now it should hit the path its supposed, then hit the htaccess file, and then route me to where IM supposed to be in this other application. Unfortunately it doesnt seem to work, after adding the rule I end up the same place - magento.
Here is what I've written:
RewriteRule ^(.*somefolder.*)$ $1 [L]
Im not sure what could be going wrong, as I think the approach seems pretty straight forward. Any ideas on how to remedy this situation?
Thanks.
Here is Your Simple Answer.Also Used By me on my site.
RewriteCond %{REQUEST_URI} !^/(yourfoldernameHERE)$

Apache rewrite rule for a whole directory

I am trying to rewrite a directory to its own sub folder. I have an .htaccess file in the directory, and I also put the AllowOverride All in the Apache conf file.
Basically, I want the server to redirect this url: http://example.com/MPOS to http://example.com/MPOS/public. The closest I got was using this:
RewriteRule /MPOS$ /MPOS/public/ [L]
It does the job, going to http://example.com/MPOS/public, but its the rest of the resources (all the pages, stylesheets and the rest) are not being redirected, and so I get the page as if the links are broken. Inside public folder there is "index.php" file.
I know there are many questions about it, but after hours of search nothing helped me, so I posted this question.
Any help would be really appreciated.
Thanks,
Shay
OK, I feel a bit stupid. Managed to get it to work by writing the rewrite like this:
RewriteRule ^$ /MPOS/public/ [L,R]
When the .htaccess is found in the "MPOS" folder.

shorter url with php

I've seen it before and I have no clue how to do it or what to look for on google to find it.
Basically I'd like to know how to get these shortened urls to work, what i mean is for example
this: http://website.com/index.php?id=666
turned into this: http://website.com/666
Is there some way to do this through php or do people actually go around making maps for each id?
thanks
Hi there depends what server you are on but if you are using a server with apache that has mod rewrite you can use a htaccess rule.
So an example would be:
first create a .htaccess file then paste in the following content:
RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L]

.htaccess 404 page not found

I'm writing my own url shortener. I'm done with everything such as creating short urls. But when I try to browse htt p://example.com/rtr93, I get a 404 error. But http://example.com/index.php/rtr93 works find and shows the relevant page (I'm not redirecting to a new url. I'm just getting the relevant record from database which has a column short_url).
I'm using PHP and syfmony 1.2 if that helps. I think I need to properly setup .htaccess file. But I don't know where to get started.
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /index.php [L]
You may want to make the regex more specific if you're planning on hosting other things on the same domain.

Categories