Problem with mod_rewrite and links - mod-rewrite

I'm developing a MVC-based application and I've now set the .htaccess rewrite rules. However, I'm having problems to make everything appear correctly.
This is my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?a=$1 [L,NS]
I'm having problems with the links as at first the URLs were like "domain.com/folder/index.php?a=whatever/whatever2/asd", but now they are "domain.com/folder/whatever/whatever2/asd".
I was generating the links just like "<a href='index.php?a=new/qwerty'>Qwerty</a>", but now when I try to generate the links I must do something like "<a href='new/qwerty'>Qwerty</a>", and guess what? If I'm, for example, on "domain.com/folder/new/lol" and I click on that kind of link, I will get redirected to "domain.com/folder/new/new/qwerty" and it isn't meant to work like that.
I can't use absolute paths as the software is meant to be easy-to-install for users who download it, and I can't know where the users will install this software, nor I want users to have to modify anything of the code.
Thanks in advanced,
Ivan

I finally solved it.
I made my own link generator function and I made it to delete the index.php part from $_SERVER['SCRIPT_NAME'] (which gives us the absolute path to the script).
In case you had more than one script, just delete every possible script from that special variable.
function url($rel_path) {
$start = str_replace("index.php", "", $_SERVER['SCRIPT_NAME']);
return $start.$rel_path;
}

Related

rewrite url only and stay on the same page

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.
I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

Custom route not working in codeigniter

I believe that I already did all the important thing to set up the custom route in codeigniter but I still don't know why I always get 404 error.
The current url I can access is: http://localhost:8080/project/api/profile_test/
And I want to rewrite it as: http://localhost:8080/project/api/users/
I've added this code in the route.php but still not working: $route[‘users’] = 'profile_test';
My .htaccess is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
I already set the AllowOverride All and remove the index.php in $config['index_page'] = '';. But still not working.
I’m almost 8 hours for this with this little thing and I need help to the one who knows it.
What am I missing? Any help is much appreciated.
In Codeigniter, a route consists of 3 parts:
the URL
the controller name
the method name
The docs explain it well.
So, if your project's root is http://localhost:8080/, for a URL like project/api/users/, to be handled by a controller (located in application/controllers/) called Profile_test and a method called index, you would need a route like:
$route['/project/api/users'] = 'profile_test/index';
Note that the controller file and class must be capitalised, as described in the docs, so for example controllers/Profile_test.php.
If your project's root is http://localhost:8080/project, you would change that to:
$route['/api/users'] = 'profile_test/index';
If your controller is located in a subdirectory like application/controllers/api/, you would change that to:
$route['/project/api/users'] = 'api/profile_test/index';
Also make sure you are using normal single quotes. Some of the code you include in your question includes 'smart' quotes: $route[‘users’] which will not work in PHP.

Joomla not finding index.php after

I had turned Use URL rewriting to ON
Added some code in to the ht access file and this never worked.
I then turned off url rewriting and removed the code and now when I try to go to the website I am getting a 404 error.
I can still access /administration
the website is www2.daxtra.com
the page it should find is http://www2.daxtra.com/index.php/home
Does anyone have any ideas what has happened?
This was the code I added:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Thanks
Right, I've done a small test.
To get what you want, you need to following:
htaccess.txt (you don't need a .htaccess file)
Search Engine Friendly URLs set to On
Use URL rewriting set to Off
You finally need to ensure that your Homepage menu items it set to a specific component such as an article or something else, not an Alias
This will ensure that you get example.com/index.php/home
Hope this helps

How to keep mod_rewrite from recognizing directories

So, lately I've been dealing with an issue relating to mod_rewrite and it seems nobody is trying to do anything like it. Every question people have is about trying to exclude directories from the rewrite, when I want them to be included like any other.
For instance, assuming my root directory with .htaccess file in it is www.example.com/root/
When I type in made up directory, such as www.example.com/root/asdfasdf, I have my .htaccess file set to redirect me to www.example.com/root/index.php?url=asdfasdf without change what's in the address bar on my browser
However, in trying to do the same with a real directory, such as www.example.com/root/admin, it not only changes the url in the address bar but changes it to www.example.com/root/admin/?url=admin.
Can anyone explain to me what's going on. I've tried all kinds of different regular expressions and flags and the ones that redirect anything still cause this same issue. can I go to www.example.com/root/admin and still get redirected to the root folder while hiding that the query string is ?url=admin.
[UPDATE: additional information 11-30-2012]
Like I said, I've tried it will multiple different lines of code and come out with the exact same redirect issue, assuming the redirect doesn't just fail altogether and produce a 500 error. Here's one of my latest iterations, though, which has produced the issue of not ignoring direcotories.
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_FILENAME} !^(.\*\\.("png"|"jpg"|"gif") [NC]
RewriteRule (.\*?) index.php?url=$1 [QSA]
The rewrite condition is to keep the engine from rewriting if a picture is being requested (for css and img tags). I only didn't mention it previously because I have tried removing that line and it has made no difference.
I'm not exactly a master of mod_rewrite, though, so if you see any errors with anything I've written, please feel free to let me know.
It's not entirely clear from your question what you are trying to do and it would have been helpful to see what your .htaccess file actually looked like. However the following lines in an .htaccess file in the root folder:
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [L]
Will silently redirect requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php and will also do the same for real folders. So if the folder admin exists under root, then requests to http://www.example.com/root/admin will be silently redirected to http://www.example.com/root/index.php?url=admin
If however you wanted to serve up folders and files that actually exist, but rewrite requests for folders and files that do not exist, then you would need to adjust the rewrite like so
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [R=301]
This would still rewrite requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php, but for real folders and files, such as requests made to http://www.example.com/root/admin, the admin folder would be served up.
Hope this helps, but if you can clarify your question a bit then I can try and help again.

URL rewriting problem

In order to convert dynamic URLs on my site www.kitesmovie.co.in to static urls. Eg: www.kitesmovie.co.in/stories.php?id=10 to www.kitesmovie.co.in/Barbara_Mori_Hrithik_Roshan_New_Movie.
I tried using rewriting rules in my htaccess files, but it did not work. Please tell me how to do this.
Thanks a lot in advance.
It's not really clear what you are trying to achieve.
When you say "convert dynamic urls to static urls", do you really want to have user to type in the ...?id=10 and the file named Barbara_Mori_Hrithik_Roshan_New_Movie lives on your server? I think, it's the opposite way - you want to let the user type in the long & nice title and actually resolve it to ?id=10
If this is the case, the #Mike's answer is nearly correct, you only have to swap the parts of the last line:
RewriteRule Barbara_Mori_Hrithik_Roshan_New_Movie stories.php?id=10 [R,L]
Another question - are you sure you have the .htaccess working? An easy way to check is to set the contents of .htaccess to
order deny, allow
deny from all
And see if you can still access that directory. If you can, this means the "deny from all" does not work. Then check your apache config - is .htaccess allowed in the particular virtualhost and/or directory.
Lots of missing info there. If you are using apache with mod_rewite installed, place this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule stories.php?id=10 Barbara_Mori_Hrithik_Roshan_New_Movie [R,L]
</IfModule>
You might want to have a look at this article: http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
Generally, you would use a RewriteRule something like this:
RewriteRule ^stories.php?id=10$ Barbara_Mori_Hrithik_Roshan_New_Movie [R=301,L]
If that's not working for you, then feel free to add more details to your question.

Resources