.htaccess URL rewriting - file loaded, but subsequent files don't work - php

I'm currently trying to use "pretty" URLs instead of using filenames through rewriting in the .htaccess.
This is what I would like to have:
site.com/dir/report1 shows site.com/dir/report.php
site.com/dir/report1/ shows site.com/dir/report.php
I don't want a redirect but just the file to be loaded without leaving the pretty URL.
The first one works, but with the all calls to e.g. stylesheets now have "report1/" as base directory and thus can't be found.
Is there any way around this?
Here's my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/?$ report.php [NC,PT,L]

With help from #arkascha I've been able to solve this by redirecting all requests to "/report1/" to "/report1". This is my code:
RewriteEngine On
RewriteBase /dir/
RewriteRule ^report1/$ report1[R=301]
RewriteRule ^report1$ report.php [L]

Related

.htaccess pretty URLs are not working

I'm trying to use pretty URLs on my site, and I don't know too much about htaccess, but after some search I ended up with this:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
I'm trying to change this:
mysite.com/cms/content.php?action=content&id=80
into:
mysite.com/cms/content/80
But somehow it's not working and what I get is a blank screen and a 404 error,
Failed to load resource: the server responded with a status of 404 (Not Found) --in-- /cms/files/plugin/tinymce/tinymce.min.js
About the 404 error: It should load a .js file from mystie.com/files/plugin/tinymce/tinymce.min.js -- not -- mystie.com/cms/files/plugin/tinymce/tinymce.min.js
My htaccess file is in the root folder. Am I doing something wrong?
I think the server did not read my .htaccess file. How do I find out the issue?
You need to place this rule in /cms/.htaccess:
RewriteEngine On
RewriteBase /cms/
RewriteRule ^([a-z]+)/([0-9]+)/?$ content.php?action=$1&id=$2 [NC,L,QSA]
Then you need add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's relative URL.
RewriteEngine On
RewriteRule ^/cms/([a-z]+)/([0-9]+)$ content.php?action=$1&id=$2 [NC]
Remove escape slashes \
You need to make sure to include the subfolder if your htaccess is in the root.
Try this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/([a-z]+)/([0-9]+)/?$ /cms/content.php?action=$1&id=$2 [NC,L]
Also a blank screen usually means a PHP error.
You have /cms/ in your URL before the rest, so you need to change RewriteRule to something like this:
RewriteRule ^cms/([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
Also, I hope you are aware that .htaccess can not work on some servers (like nginx or some custom ones), and even on Apache servers there can be mod_rewrite disabled.
My guess is that your scripts and styles are being linked to via relative URLs. Because the old URL is /cms/content.php the URI base is /cms/ and your relative links all resolved to be under /cms/, which I assume is what you want.
However, when you change your links to /cms/content/1234, the URI base becomes /cms/content/ and all relative links will use this as its base, and since "content" isn't even a real folder, all those links will 404, and you won't load any of your CSS content or scripts.
Either change your links to absolute URLs or specify a URI base in the header of your pages (inside the <head> </head> tags):
<base href="/cms/" />

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

Remove in between folder structure from the url in a php website

I have a php website having following folder structure (basic structure).
project_name
app
controller
model
view
css
js
img
index.php
So when I view index.php in WAMP the url is http://localhost/project_name/
But when I go inside the site (eg. login.php which resides under view folder) url is like this. http://localhost/project_name/app/view/login.php
I found that using .htaccess we can change the urls. So I tried this (in .htaccess).
RewriteEngine on
RewriteBase /
Redirect 301 /project_name/app/view/login.php /project_name/login.php
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
Now url is http://localhost/project_name/login.php It is correct. But it seems php does not use the original link to grab the file (ie. from /project_name/app/view/login.php) but from here /project_name/login.php
So it throws 404 error.
What should I change? Please help me, i am just trying to hide /app/view/ part from the url so that user won't see my folder structure. I have read about various ways of doing that for about 9hrs today but still couldn't get anything working correctly.
Hope my question is clear enough. Any help is greatly appreciated!
URI's passed through rewrite rules in an htaccess file has the leading slash removed, so your rule:
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
won't ever match anything because of ^/. Also, since you are rewriting to a URI that matches a previous redirect, your browser will see a redirect loop and say that it's not redirecting properly. You'll need to match against the actual request and not the URI:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project_name/app/view/login\.php
RewriteRule ^ /project_name/login.php [L,R=301]
RewriteRule ^/?project_name/login.php$ /project_name/app/view/login.php [L]

php:Mod rewrite giving me hard times finding base address.. need help

im new to Mod_rewrite but problem is it ruins all my ajax and php
i use windows.location object to get current location for javascript example:
var str = location.pathname;
str=str.slice(0,str.lastIndexOf('/'));
problem that this never works since i used mod_rewrite.
my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
can some body lead me to way to fix this. ?
also i face same problem with lading all imgs and css/.js but i heard i can write condition on .htaccess to only redirect .php !,
I need a way to find base address for ajax calls, and other way to find base address for php.
the problem is that you are redirecting all your url to a php file and it breaks all the other assets, including css and js.
Solution:
Use a rewrite condition. this way it only redirects if the file or directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
the first line is : if file doesn't exist
the second is: if directory doesn't exist
the there will be a redirect.

Categories