URL rewriting PHP losing querystring - php

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

Related

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.

.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.

Reroute URL parameters to path and keep HTML references working (htaccess)

I have a URL http://example.com/?r=FOO&c=BAR which I want to access via http://example.com/FOO/BAR. Both parameters are optional. I got this working using this htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)(.*)?$ index.php?r=$1&c=$2 [L]
However, now I realized that files referenced in my index.php will not load. For example I have a http://example.com/css/app.css. With the current rewriting of the URL the browser tries to get http://example.com/FOO/css/app.css and receives the actual index.php.
I tried adding <base href="/"> and <base href="http://example.com/">.
I'm stuck here. Anyone knows how to solve this? Thanks in advance.
You need to ignore files by adding this condition under your RewriteBase:
RewriteCond %{REQUEST_FILENAME} !-f

URL Rewriting returns 500 internal server error

I am trying to convert this:
site/read.php?id=6
to
site/read/6
I have tried a couple of solutions found on SO, with the last one being (to output: site/read/id/6):
RewriteRule ^(.*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
When I try the second link, it will hang, and apache crashes (LOL).
Not sure if it has a problem with the rest of the .htaccess file, so here is the full code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
I could achieve (with rows 1-4) that no php extensions are showing up, so the address bar currently reads site/read?id=6
Can you please point me out where have I gone wrong?
You can put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/read\.php\?id=([0-9]+)\s [NC]
RewriteRule . /read/%1? [R=301,L]
RewriteRule ^read/([0-9]+)$ /read.php?id=$1 [L]
I would do dthis in PHP , it is very hard to debug what Apache is getting , it may be that it goes into indefinite loop, did you find anything in apache log files ?
But save yourself a trouble and do it in PHP, if you are using a Framework it is easy to inlcude one PHP that checks for this and redirects to new page if it detects .php in url.

mod_rewrite /$variable/file.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]

Categories