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

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

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.

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

url re write issue in .htaccess file

Here is my problem
in my website base url am redirecting to an inside folder
eg: http://example.com/
to
http://onlinevyapari.com/business/ by using this code in .htaccess file
RewriteRule ^(/)?$ /business/index.php [L]
now I want to use search engine friendly url for the same website my query is like this
http://example.com/business-details.php?id=106
but I want to keep my url like this
http://example.com/business-details/106
I have done in different way like bellow
RewriteEngine on
RewriteRule business-details/id/(.*)/ business-details.php?id=$1
RewriteRule business-details/id/(.*) business-details.php?id=$1
its happening but css is not loading properly
it will be really appreciable to me if somebody help.
thank you
Have your rule like this:
RewriteEngine on
RewriteRule ^(/)?$ business/index.php [L]
RewriteRule ^business-details/(\d+)/?$ business-details.php?id=$1 [L,QSA,NC]
For solving css/js/image path issues just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
You need to add the following, before your rules, to make the rewrite rules ignore file and directory names.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

.htaccess url rewriting issue - redirecting every request to sub directory

i have following htaccess file which redirecting every link to /love/tribe-world directory (including root also).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^/campaigners/)^(.*)$ /love/tribe-world/campaigners/$1 [L,QSA]
i have written above rules for hiding /love/tribe-world directory from url. i wanted url www.example.com/campaigners/start-process instead www.example.com/love/tribe-world/campaigners/start-process, which is working using above rule.
But its redirecting every request to the /love/tribe-world which is strength for me.
Can anyone guide me what i am doing wrong here...
Thanks in advance
Your lookahead is incorrect. Use this rule:
RewriteEngine on
RewriteRule ^(campaigners.*)$ /love/tribe-world/$1 [L,NC]

.htaccess File, path is redirected properly but images and css are not loaded

Hi I am using the following lines in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /coaster/CoasterInsider/
RewriteRule signup$ index.php?page=signup [L,NC]
RewriteRule login$ index.php?page=loginHandle [L,NC]
RewriteRule u/(.*)$ index.php?page=profile&username=$1 [L,NC]
The path is getting redirected properly, but the css,flash and images are not loading. Also if I use the following '/' after any url say,
RewriteRule signup(/?)$ index.php?page=signup [L,NC]
It's not finding any page there, i.e. error 404. I just want my htaccess file to work for both /signup and /signup/
When browser displays:
http://example.com/coaster/CoasterInsider/signup
http://example.com/coaster/CoasterInsider/signup/
and encounters a relative URL such as:
<img src="site/images/photo.png">
It translates the relative URL to (respectively):
http://example.com/coaster/CoasterInsider/site/images/photo.png
http://example.com/coaster/CoasterInsider/signup/site/images/photo.png
You should use absolute URLs for assets. Make this a habit:
<img src="/site/images/photo.png">
Alternately you can use the HTML base tag in your pages which tells browsers how to treat relative URLs. Personally I do not recommend it.
This was advised to, and worked for me, place it inside the <head> tags in your html: <base href="/"> Again, it worked for me, I hope it helps someone else.
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to exclude real files and directories from being rewritten.
You could also add
RewriteCond %{REQUEST_FILENAME} !-l
to do the same for symlinks.

Categories