Rewriting a URL to a PHP parameter from inside a subdirectory - php

What I'm asking for is simply rewriting any URL-given sub-directiories to a PHP URL parameter for nicer looking, user friendly URL.
I know that this is possible using the Apache Rewrite Engine and associated parameters.
The problem on my end is that I do not want to rewrite i.e
mydomain.com/parameter ----------->
mydomain.com/index.php?id=parameter
But rewriting
mydomain.com/subdirectory/parameter ----------->
mydomain.com/subdirectory/index.php?id=parameter
In my root folder I have an .htaccess file for 404 errors and such, redirecting non-existant pages to the main one. During the tests I've done I have deactivated these thinking that they could overwrite the other .htaccess file.
I tried...
RewriteEngine On
RewriteRule ^directory/([^/\.]+)/?$ index.php?id=$1 [L]
...and other snippets similar to that one.
The .htaccess file containing this code was placed in the root/directory folder.
What I wanted to achieve with that was to redirect root/directory/string to root/directory/index.php?id=string.
Since my knowledge of .htaccess and rewriting is obviously limited, I need answers to two questions.
Will a 404 rewriter in the root folder overwrite any other rewriter in a subdirectory?
How do I achieve what I'm after? (much like how url-shorteners work - bit.ly/shortened) from a subdirectory?

This rule should work for you in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(directory)/([^/.]+)/?$ /$1/index.php?id=$2 [L,NC,QSA]
Make sure there is no other .htaccess file in directory.

Related

.htaccess Completely rewrite URL to show file in a folder, rather than page in root

So, I have the current file structure:
ROOT
-> /public
-> /user_views
user_handle.php
user_profile.php
user_feed.php
user_settings.php
.htaccess
As you see, the folder user_views contains a few of the possible views that the client could want to look at. What I am wanting, is for clients that insert the URL http://example.com/user/ to be directed to the page user_handle.php. This handle would act as a root file for all /user/ pages, and it would accordingly split into those pages through numerous $_GET requests.
So far, I have the following .htaccess, but it's not working...
RewriteRule ^user/ user_views/user_handle.php [L]
What could I do to get this to work, so that the url http://example.com/user redirects to the user_handle file in the user_views folder?
Thanks!
I'm not sure I fully understand your question, but it seems you would like to make user_handle.php located under public/user_views act as a "router" for the rest of you PHP files and have all requests to /user/ (e.g. /user/?page=1) be processed by user_handle.php.
If that's the case, your rule seems legit. The only thing I noticed (I might be wrong) is that your .htaccess is located outside the public folder. In that's the case, you need to include 'public/' as part of your rule.
I recreated the folder/file structure you described and it has worked for me using the following .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/ public/user_views/user_handle.php [L]
</IfModule>
Slight chance this is the problem, but you also might want to double check that mod_rewrite, which is the rule-based rewriting engine is enabled on your server/local environment. It should show up under 'Loaded modules' when you call phpinfo() in any PHP file.
Hope this helps.

How To Rewrite URL in .htaccess?

I have a site/blog that pages URL as www.example.com/sample-page and I want to make it like www.example.com/p/sample-page.html For this purpose I am thinking to use .htaccess file to write rewrite/redirect rule to do it. So can we make it using .htaccess?
You probably should be looking at the mod_rewrite documentation for apache (the rules in there can be placed into a .htaccess file).
http://httpd.apache.org/docs/current/rewrite/remapping.html
However, for your specific case (which I think is to end up mapping the real URL www.example.com/WP/p/sample-page.html to www.example.com/WP/sample-page), you could do something like this:
RewriteEngine on
RewriteRule ^WP/sample-page$ WP/p/sample-page.html [NC]

htaccess rewrite rule preventing access newly added php files

I have a website with htaccess file. the htaccess has lot of codes. when I create a new php file it redirects to another page. so I checked my htaccess file and remove a one line of code
RewriteRule ^.*\.{1}(htm|html|php)\/? index.php [L,QSA,NC]
after that the file can can able to access diretly. can anyone know the meaning of above htacess and how can it effect for newly added php files.
This rule:
RewriteRule ^.*\.{1}(htm|html|php)\/? index.php [L,QSA,NC]
is silently forwarding every request URI with extension .php OR .htm OR .html to index.php
So even if you add a new .php above regex ^.*\.{1}(htm|html|php)\/? will match and request for the newly created file will still be forwarded to index.php
PS: This rule doesn't make lot sense though a similar looking rule is used in many front controllers like Wordpress, CodeIgnitor, Cake etc frameworks.
Reference: Apache mod_rewrite Introduction

.htaccess remove extension while preserving uri parameters

i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.
If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]
Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]

.htaccess - mod_rewrite is working for some pages but not for others

I am stuck into a weird problem.
I have a file at the location /public_html/academics/courses.php
I want .htaccess to mod_rewrite the URLs as below:
Original URL: http://niecdelhi.ac.in/academics/courses/
After mod_rewrite: http://niecdelhi.ac.in/index.php?inc=/academics/courses/
What I want, basically, is to mod_rewrite all URLs to index.php and pass the URL as a parameter named "inc". Then, in the index.php I include the file by doing include($_GET['inc']);
mod_rewrite is working for some pages on the website. and I am getting the URL in $inc. But, it is not working at all for other pages.
For example, consider the two files that exist on the server:
http://niecdelhi.ac.in/academics/courses.php
http://niecdelhi.ac.in/academics/library.php
mod_rewrite is working for the first, the file gets included in index.php
But for the second I get the plain existing file. not the one included in index.php
I hope you understand the problem that I am facing. Please provide me with the solution.
.htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\..*$ [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule ^(.*)$ index.php?inc=$1&%1 [L]
Page working: http://niecdelhi.ac.in/academics/courses/
Page not working: http://niecdelhi.ac.in/academics/library/
*EDIT*
There is no other .htaccess anywhere. Although, I have found a clue about what is happening. The problem is happening only in Linux server. The code is working correctly in Windows server.I have a Linux server with PHP 5.2.16.
Also, regarding some pages working and some not. I have found that only those pages are working which have a folder with identical name in the same directory. For example, The academics directory is as below:
academics/
|_ courses/
| |_ mba.php
| |_ mca.php
|_ courses.php
|_ library.php
Now, Since courses.php has a folder with identical name in same directory. It gets mod_rewrite fine. But library.php is not getting mod_rewrite.
Linux server is skipping the mod_rewrite for the files that actually exist. Why so ??
My only guess is that there is another .htaccess somewhere in your structure (most obviously inside /academics/ ) that is overriding the rule for "library".
Could there be another mod_rewriting rule inside that folder that is kicking in for the word LIBRARY and probably messing up your rewriting.
Note that it might also be a native apache issue. For example, in ubuntu, by default in version 10.10 (i think thats it) if you had a /javascript/ folder, it would be short circuited to /usr/lib/javascript or something like that...
Check all possible instances of mod rewrite in httpd.conf, all dynamicaly loaded .conf files, your vhost file and finaly the path of your document root...

Categories