I am developing on my localhost and i have a structure like so:
|public_html
| .htaccess
| [pagefolder]
| ......index.php
| ......[adminfolder]
| .............htaccess
| .............admin.php
As you can see, i decide it not to go crazy on .htaccess files. Just 2 is enough. One on my root and one on my protected admin folder.
when the url is localhost i just do the following on my root .htaccess to display the initial page:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?$ pagefolder/index.php [L]
When i want to access the admin panel (url: localhost/admin), i just do:
RewriteRule ^admin/?$ pagefolder/adminfolder/admin.php [L]
So far so good (i think). I run into problems when im using the .htaccess in the [adminfolder]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^award/?$ admin.php?v=award [L,QSA]
When i go url: localhost/admin/award i get error:
The requested URL /admin/award was not found on this server.
However, the rule in the [adminfolder] looks ok to me. Im guessing something is missing, but not sure what?
None of your rules match /admin/award.
Your last rule redirects /pagefolder/adminfolder/award to /pagefolder/adminfolder/admin.php?v=award.
You can fix this by redirecting more admin URLs:
RewriteRule ^admin(/.*)? pagefolder/adminfolder/$0 [L]
Then change your adminfolder .htaccess:
RewriteRule ^admin/?$ admin.php [L,QSA]
RewriteRule ^admin/award/?$ admin.php?v=award [L,QSA]
Related
I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]
I'm trying to create a htaccess rule to rewrite the following URL's:
localhost/abc/def/1 (or with trailing slash would be fine)
to
localhost/abc/def.php?id=1
accessing just
localhost/abc/def/ is also valid, and it's working.
My code so far:
RewriteEngine On
Options +FollowSymLinks
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index$ ./src/index.php [NC,L] # it works
RewriteRule ^def/?$ ./src/def.php [NC,L] #works too!
RewriteRule ^def/(.*)?$ ./src/def.php?id=$1 [NC,L] #this doesnt
I've read some posts like this one Rewrite Rule To Detect Numbers Only but they didn't work for me.
The weird thing is, if i change the slash between def and the id part with underline, it does work, like this:
RewriteRule ^def_(.*)?$ ./src/def.php?id=$1 [NC,L]
But the URL becomes localhost/abc/def_1 and doesnt look that good.
UPDATE:
Site structure:
/home/username/public_html/abc/ (have multiple websites on public_html)
|_media/
|____js/
|____css/
|____images/
|_src/
|____def.php
|____fgh.php
|_.htaccess
When the rule to redirect def/1 does work, the media url's within the page are also being translated to:
http://localhost/abc/def/css/bootstrap.min.css
Thanks!
Reproducing your case scenario, I've made the follow folder structure:
/home/username/public_html/
|_ abc/
|___ .htaccess
|___ src/
|_____ def.php
In my .htaccess located inside the abc folder, I have the following:
RewriteEngine On
RewriteRule ^def/?$ src/def.php [NC,L]
RewriteRule ^def/([1-9]+)/?$ src/def.php?id=$1 [NC,L]
And all the scenarios you mentioned work as expected.
These 2 go to def.php:
domain.com/abc/def
domain.com/abc/def/
These 2 go to def.php?id=1 and def.php?id=3 respectively:
domain.com/abc/def/1
domain.com/abc/def/3/
I am afraid you might have additional .htaccess or rules taking effect or even your browser cached not giving the proper information you want.
Try using a different browser and try the above exactly as I described it.
How can I translate an URL like:
http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
to:
http://localhost/mySite/OFFERS/ARTICLE/DETAIL
if one parameter is empty it should still work like this:
localhost/mySite/?link=OFFERS&sublink=ARTICLE
localhost/mySite/OFFERS/ARTICLE
Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.
Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L]
But that only works for one parameter and I couldn't improve this code for ages ^^
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]
Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE.
Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).
Try adding the following to your htaccess file in the mysitedirectory of your site.
RewriteEngine on
RewriteBase /mysite/
# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]
I have a shared hosting in which I want to host several websites. To this end, and to keep things clear, I have redirected my primary domain (i.e. www.mydomain.com) to a folder (i.e. /mydomain.com/). However, I want to prevent direct access to this folder (www.mydomain.com/mydomain.com/) from the URL with a 404 error (not found).
This is the .htaccess in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain.com/0.1/$ [NC]
RewriteRule ^(.*)$ /mydomain.com/0.1/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain.com/0.1/index.php [L]
RewriteCond %{THE_REQUEST}% ^[A-Z]{3,9}\ /mydomain\.com/ [NC]
RewriteRule . - [R=404,L]
And this is the .htaccess in /mydomain/ with a bunch of rules for nice and tidy URLs.
RewriteEngine On
RewriteRule ^about/(.+) index.php?a=about&b=$1
RewriteRule ^services/(.+) index.php?a=services&b=$1
Right now this is the result:
Accessing www.mydomain.com shows the content in /mydomain/ as hoped. However, www.mydomain.com/mydomain/ is also displayed, ignoring the 404 rule, and creating another folder -whatever the name- without an htaccess DOES throw the 404.
I've been dealing with this problem for 5 days now and tried everything. At this point I don't know if the error comes from the root .htaccess or the folder's.
Thanks in advance.
PS: Just to be clear, I have no control over the httpd.conf file.
Notes:
I forgot saying that there is an additional folder inside /mydomain.com/ called /0.1/ for version control.
If a include "RewriteOptions Inherit" after "RewriteEngine On" in the /mydomain.com/0.1/ htaccess, I get a 500 internal server error.
Deleting the /mydomain.com/0.1/ htaccess file altogether will produce the desired 404 error for www.mydomain.com/mydomain.com/0.1/ and produce a 505 in www.mydomain.com
The R flag in rewrite rule should be something between 301 to 399 , so it can not be 404!!!
your rule should be like below:
RewriteCond %{THE_REQUEST} ^/mydomain\.com/? [NC]
RewriteRule (.*) throw_404.php [L]
create throw_404.php and to send 404 status code! and add L flag to your first rule for avoiding conflicting between that rule and mine!
I really have only a fair idea of what I am doing. Sorry.
This is what I did to rewrite URLs like these:
http://example.com/foo/bar/news/1-category-name/2-item-name
http://example.com/foo/bar/news/1-category-name
into these:
http://example.com/foo/bar/news.php?newsID=2
http://example.com/foo/bar/news-categories.php?categID=1
respectively
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /foo/bar
RewriteRule ^news\/([^\/]+)/([0-9]+)-([^\/]+)$ news.php?newsID=$3
RewriteRule ^news\/([0-9]+)-([^\/]+)$ news-categories.php?categID=$1
</IfModule>
But whenever I try this URL:
http://example.com/foo/bar/news/16-news-category-1/11-title
It always returns a 404 error:
The requested URL /foo/bar/news.html/16-news-category-1/11-title was not found on this server
Why is it trying to find news.html? What am I doing wrong?
I don't know if this is relevant but example.com is virtual (I really don't know the term). The actual files can also be accessed via http://real-domain.com/qux/. So you'd do http://real-domain.com/qux/foo/bar/news/1-category-name/2-item-name to get to 2-item-name.
Does it also help to say that this script actually runs on my development machine?
Development:
PHP v5.3.5
Apache v2.2
Live:
PHP v5.3.6
Apache v2.2
You this code in your .htaccess:
RewriteEngine on
RewriteRule ^news/([0-9]+)-([a-z\-]+)-([0-9]+)/([0-9]+)-([a-z\-]+)(\/?)$ /news.php?newsID=$4 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)/([0-9]+)-([a-z\-]+)(\/?)$ /news.php?newsID=$3 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)-([0-9]+)(\/?)$ /news-categories.php?categID=$1 [L,NC]
RewriteRule ^news/([0-9]+)-([a-z\-]+)(\/?)$ /news-categories.php?categID=$1 [L,NC]
so when someone goes to:
news/11-category-12 or
news/11-category-12/ or
news/11-category or
news/11-category/
it goes to: news-categories.php?categID=11
and when someone goes to:
news/11-category-12/1-title or
news/11-category-12/1-title/ or
news/11-category/1-title or
news/11-category/1-title/
it goes to: news.php?newsID=1
Also, make sure the .htaccess is at the root of the public_html folder as well of news.php and news-categories.php