mod_rewrite /$variable/file.php - php

I've been trying to attempt this, but I am unable to yield any results. I'm trying to rewrite a variable into a "directory" in the url to multiple files.. I better explain this is what I mean.
Without the rewrite:
/index.php?var=test
/admin/panel.php?var=test
/faq.php?var=test
/directory/test.php?var=test
With the rewrite:
/test/index.php
/test/admin/panel.php
/test/faq.php
/test/directory/test.php
I'd like to be able to pass other variables to the files as well.. example:
/test/index.php?user=1&session=1233445
I realize that I could do a rewrite rule for every physical file and directory.. but I want it to be dynamic so if I add files to my site I don't have to keep editing the .htaccess file. Any help would be appreciated. Thanks!

Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*?/?)(index|panel|faq|test)\.php$ /$2$3.php?var=$1 [L,QSA]

Related

additional .htaccess in subfolder | mod_rewrite for query parameter

I have a little mod_rewrite problem and really need some help.
I need to have a second .htaccess file in a subfolder for a project on my domain.
The URL to the project is like:
https://example.com/project-name/
Now I have a query parameter for which I want to create a nice URL with mod_rewrite.
The URL including the parameter looks like this:
https://example.com/project-name/index.php?preset=nameofapreset
And I want it to be:
https://example.com/project-name/nameofapreset
The important thing: The mod_rewrite has to work for the current folder only. Without having any info about the current path, cause I need to use the file in other folders, too.
That's what I thought should work (cause I am using a similar rewrite rule in another project):
RewriteEngine On
RewriteRule ^([a-z])/?$ index.php?preset=$1
But it doesn't. And all my trials fail.
Any ideas how I can make it work?
Thanks!
You can use this rule in /project-name/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?preset=$1 [L,QSA]

How do I strip 'public_html' and parameters from URL using .htaccess

I have come across a problem, where my whole web-project is under a subdirectory of a subdirectory in my web server, therefore the 'public_html' folder in there does not fall off from the URL.
Is there a way to do this strip via .htaccess in a clean way?
Here is an example of my URL structure:
http://myurl.domain.com/portfolio/projects/MyProject/public_html/?page=home
I would also like to perform some cleaning to my URL by stripping off the ?page= parameters, but I'm running a PHP config with an array which sets the <title> of the current page by identifying the current parameter in use with $_GET.
Second question is; can I strip those parameters off or will that break my title-setup and if I can and it will not break anything, how am I supposed to do it?
Thanks in advance, I'm a total newbie when it comes to .htaccess and any help and advice granted I will hoard to my knowledge!
In your root htaccess file ,try adding this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /portfolio/projects/MyProject/public_html/\?page=([^\s]+) [NC]
RewriteRule ^ /portfolio/projects/MyProject/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?portfolio/projects/MyProject/([^/]+)/?$ /portfolio/projects/MyProject/public_html/?page=$1 [QSA,L,NC]
This will redirect "/portfolio/projects/MyProject/public_html/?page=pagename" to "/portfolio/projects/MyProject/pagename" stripping out the public_html folder and querystrings.

URL rewriting PHP losing querystring

I have a dynamic URL like
<domainname>/sub/cl/<phpfile.php>?c=123
phpfile.php can be any php file which comes in run time
/sub/cl/ is a folder path.
i have use this code in .htaccess:
RewriteRule ^sub/cl/new-file/([0-9]+)/?$ sub/cl/newfile.php?c=$1 [NC,L]
this is correctly redirecting to the newfile.php when i hit /sub/cl/new-file/321/ but sub/cl/ get appended before all css and js which are in root and also c=321 get lost
Please help me what m doing wrong.
Thanks in advance
if your problem is only query string insert [QSA] flag to to the rule. JS/CSS/IMG files are losing because the rewrite works for every request. Over come this issue by adding RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f #Files
RewriteCond %{REQUEST_FILENAME} !-d #Directories
further reading
Apache Mod_rewrite

.htaccess is reloading some files twice

My website is a custom made PHP website. I recently made it SEO friendly by copying the .htaccess file from wordpress and modify it a little. The problem is that some pages are realoaded twice especialy pages that have more than 1 backslash like download/something/ . I have noticed this when tracking pageviews one pageviews is counted as twice and i've done a lot of research regarding this and the pageviews are working good it's a very simple function that inserts a new row each time you view a page.
Things to keep in mind:
My website is inside some folders 'https://localhost/simbyone/sim/index.php'.
I don't want to use any GET variables i will have my variables from the URL string
I want .htaccess to look for existing directories located inside 'https://localhost/simbyone/sim/' and if it doesn't find any open index.php with all the strings attached something like 'localhost/simbyone/sim/blabla/' but inside index.php
everything works exactly as i said above the only thing that doesn't do right is that it double loads some pages
this is my .htaccess file:
RewriteEngine On
RewriteBase /simbyone/sim/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /simbyone/sim/index.php [L]
Thanks in advance to anyone that will help me.
Place the .htaccess inside your base folder (in your case, simbyone/sim).
Just put this in the file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php
This will rewrite and request that is not an existing file or folder to the index.php file.
There you can use $_SERVER['REQUEST_URI'] to evalute the request (keep in mind that the prefix "/simbyone/sim/" will be present there.

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories