Rewrite all directory requests to a subdirectory with .htaccess - php

I am trying to rewrite all requests made to /myproj(or any files inside it) to /myproj/web using .htaccess.
The problem I am having is that the requests with a trailing slash (e.g., /myproj/somefile.php/) are somehow being rewritten to /docroot/index.php instead of /myproj/web/somefile.php/.
My /myproj/.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /myproj/web/$1 [QSA,L]
</IfModule>

Take a look at this answer. It is the same problem I had, answered by a dear peer over at stackoverflow.

Related

Using apache mod_rewrite to redirect to php file

I'd like to redirect the following url:
http://www.example.com/food/?p=11&q=22&r=33
to
http://www.example.com/food/api.php/DBNAME/?p=11&q=22&r=33
As you may have noticed, DBNAME is the name of a database.
With the API I'm using it is necessary to include it in the URL.
Here's a brief folder structure of my program:
food/
->include/
->models/
->.htaccess
->api.php
->config.php
->test.php
I tried writing the rules on the .htaccess file but I get an Internal server error.
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ api.php/DBNAME/$1 [L]
</IfModule>
I can get my head around achieving the redirection.
I also tried
RewriteRule ^food/(.*)$ api.php/DBNAME/$1 [L]
but this rule just lists the files in the directory food.
Any ideas?
Thanks!
PS: The query string is not just p=11&q=22&r=33 it may use any letter or word such as type, quantity and so on. p=11&q=22&r=33 is just an example.
Edit
From the link that #sanj provided I changed my .htaccess ffile to this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/food/?$
RewriteRule ^(.*)$ api.php/DBNAME/$1
</IfModule>
It works partially. Why partially?
I have two servers, one for testing and one for production. It works well on the testing server but not on the production server. I get an Authorization Required error. I believe it's due to the configuration in my .htaccess because if I remove the mod_rewrite module and access the url directly it works. Any ideas?
Found my mistake. The folder was protected by Basic Authentication. I needed to include login parameters in my code. I can't believe I overlooked that.
I think this will work:
<IfModule mod_rewrite.c>
RewriteBase /food
RewriteEngine on
RewriteCond %{QUERY_STRING} /DBNAME/
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^$ index.php/DBNAME/?%1 [L]
</IfModule>

Rewrite URL with multiple unset query parameters .htaccess

I have read lots of questions regarding URL rewriting with multiple parameters. However I still cannot fix this issue:
I have this URL with multiple parameters, some of them without a value
http://localhost:8888/coches/index.php?maker=all&model=&registration_year=&price_until=&tfseekfid=main_search&s=~
I would like to rewrite it to http://localhost:8888/coches/all-cars
I have tried this rule and several other ones but canĀ“t get it to work.
RewriteRule ^all-cars$ /index.php?maker=all&model=&registration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
Can anyone help? Thanks
Location of the .htaccess file containing this rule is important. You can have .htaccess files in sub-directories.
If the .htaccess file is in your docroot you need to specify the directory in your rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^coches/all-cars$ /coches/index.php?maker=all&model=&registration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
</IfModule>
If the file is /coches/.htaccess, remove the leading / from your rule to make it relative to the sub-directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^all-cars$ index.php?maker=all&model=&registration_year=&price_until=&tfseekfid=main_search&s=~ [L,QSA]
</IfModule>

redirect index.php url to root domain - not working

Dear stackoverflow community users,
Here i am little bit trouble with url redirection problem.
Right now my webpage urls with index.php (http://www.mystore.com/index.php), and i am trying to redirect it on root domain with htacess file.
I place right code into htacess file but dont seems to work also if i make that blank. It also seems to not work, Website is also going to open with http://www.mystore.com/index.php url.
How can i make it proper?
Could you try code below?
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>

PHP clean URL with no php extension

This is my .htaccess code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=$1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.in$
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L]
ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
I need a clean URL for my website.
I've consulted lots of tutorials and forums and created the above code.
But it's not working. Almost I'm fighting with code.
I don't understand the clean URL concept clearly. Are there any codings I have to write in my PHP file?
<a href='movie.php?name=titanic'> Titanic </a>
I have this link in my index.php file.
I'd like example.in/movie/titanic when I click on the link "Titanic".
Also I'd like to get the value by $_[request].
What exactly do I have to do? Please don't mark this question as duplicate, I've searched a lot and haven't got the concept clearly. Please help me out.
Thanks.
The rules in .htaccess work on url's relative to the directory your .htaccess file is in. If you have a .htaccess in your www-root, then the first argument of RewriteRule will match everything behind the domain name and before the query string (movie/titanic in http://example.com/movie/titanic?is=amovie). To fix the rule, you need to change the rule to:
RewriteRule ^movie/([a-z0-9]+)/?$ movie.php?name=$1 [L,NC]
The [L] flag stops rewriting for this round, the [NC] flag ignores case.
Besides that, you only need one RewriteEngine on, which needs to be above all rules you write. You can safely delete the second one.
You can put your .htaccess into main Directory:
RewriteEngine On
RewriteRule ^movie/([a-zA-Z0-9]+)(/)$ movie.php?name=$1 [L]
Or in movies/ directory:
RewriteEngine On /movies/
RewriteRule ^([a-zA-Z0-9]+)(/)$ ../movie.php?name=$1 [L]
While movie.php is in main Directory located.
RewriteEngine On can by starterd only once per htaccess.

Concrete5 project throws 404 on all pages except index.php

I just copied a concrete5 project over to my local box and am trying to get it running on localhost. I had permissions problems at the start so I just ran a sudo chmod 777 -R site which remedied that. I then had problems with clicking a link. Anything besides index.php would result in a 404. So I messed around with the htaccess file. It came in this state:
# Use PHP53 as default
AddHandler application/x-httpd-php53 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php53/lib
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
and I changed it to this:
# Use PHP53 as default
AddHandler application/x-httpd-php53 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php53/lib
</IfModule>
<Directory>
AllowOverride All
</Directory>
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
Now, none of that helped. I decided to just disable pretty URLs in the database. I did that and most links work. It looks like some still don't (maybe the ones that are involved in a package. I don't know because I am picking up this project where someone left it). Bear in mind this all works perfectly on the live server at this time with the first .htaccess file I showed.
Any idea as to how to remedy this?
It does sound like you are having an issue with Pretty URLs. The ones that are still not working after you disabled it are likely do cached items like blocks. If you clear you cache your site will likely work without Pretty URLs turned on.
As for why Pretty URLs are not working, I would first make sure that mod_rewrite is enabled. Then you can try a couple of these for the rewrite rule.
RewriteRule ^(.*)$ index.php [L]
RewriteRule .* index.php [L]
Note: you can use the scripts from your admin login to update "pretty URLs". One caution would be is that if you use .htaccess for another purpose on your site.
Try removing the htaccess completely. Rather than deleting it, rename it. Then get into the dashboard directly at /index/dashboard/ , disable pretty urls, disable and clear the cache, and clear your browser cache.
(In general, always disable pretty urls and disable and clear the cache before cloning a site.)
Once everything is working without pretty urls, you can then start re-enabling.
I've just hit this one whilst trying out concrete5 on my home machine (linux/apache).
In my case, the problem appears to have been because I'd installed concrete5 two directory levels beneath the document root (/var/www/html). Initially, I just created the .htaccess file based on the values given when I enabled pretty urls, then hit the "page not found" problem (couldn't even log in.) Eventually, the penny dropped: I edited .htaccess and changed RewriteRule to point to the actual location of the concrete5 index.php relative to /var/www/html.
So in my case, concrete5 is installed into /var/www/html/playpen/public_html - so the new RewriteRule reads:
RewriteRule . playpen/public_html/index.php
and that appears to work.
Hope it helps someone ...

Categories