Apache mod_rewrite help on shared server - php

I'm using a shared hosting server with multiple domains, and my main .htaccess (inside the public_html/ directory) redirects all traffic to the server to my main domain (fine). The problem becomes now that I have Wordpress running on the main domain that the subfolder is being included in the %{REQUEST_URI} variable. So my .htaccess code:
RewriteRule .* https://%{HTTP_HOST}/index.php?%{REQUEST_URI} [L,R=301]
moves:
http://www.tonydiloreto.com/about/
to:
https://www.tonydiloreto.com/?/tonydiloreto/about/
(I put the ? in there so I could see what is being stored in the variable). My question is, how do I use regex to remove the additional /tonydiloreto from the %{REQUEST_URI} variable when Rewriting? If this were PHP I would do:
$new_url = str_replace("/tonydiloreto", "", %{REQUEST_URI});
If that makes sense. If you want to see what I'm talking about just go to my site now (tonydiloreto.com) and put anything after a trailing slash.
Thank you!

Not really sure what the issue here is... You simply specify a request rule which captures only part of the uri. There are millions of examples for this...
RewriteEngine on
RewriteRule ^/tonydiloreto/(.*)$ https://%{HTTP_HOST}/index.php?$1 [L,R=301]
This is documented, obviously: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Note: the above is the version for the host configuration. If you really have to use .htaccess style files, then you have to remove the leading slash (/) from the pattern, since such files work on relative URIs. .htaccess style files are notoriously error prone, hard to debug and really slow the server down.

Related

How redirect to subfolders using .htaccess file?

I'm new in php and trying to build my website.
I have created folder structure on my server listed below.
development (folder)
admin (folder)
.htaccess ( copied from internet)
index.html (file with coming soon banner)
I want to set my .htaccess file to load index.html file by default when users open (abcd.com).
And subfolders would access with hitting the url like abcd.com/admin or abcd.com/developement.
But I did some stupid changes in .htaccess file so it redirect the users to my admin login page which i don't want them to see. ;(
Thanks in Advance.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /admin/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteRule ^(/)?$ /admin/web/index.php [L]
It is a bit unclear why you actually ask this question...
As a web developer you surely should be able to find your way around the rewriting options the apache http server offers by looking into the documentation. But even if not: as #AmanjotKaur pointed out the default behavior of that http server is to serve the index.html file if present. If that is not the case then you should take a look at the DirectoryIndex directive offered by mod_dir... Also it should be easy to simply roll back to an older version of that dynamic configuration file you used (".htaccess"), if you are using a revision control system for your development as you certainly should. Or by using a simple backup which you hopefully have ...
A general hint here is to simplify the situation: instead of trying to implement rules to direct incoming requests simply separate the contents. Your "production site" (which I understand is not usable yet) should not host any content, but just that placeholder you already have. Development should not be done on the same site! Use a separate http server for that or at least a separate host name inside the same http server. That is much easier and offers much more reliable protection.
Anyway, there are situations where one indeed wants to redirect all incoming requests to some specific document or router script, with a few explicit exceptions. This might be what you are looking for here. Since your question is a bit vague in that (your question mentions the path /admin, but your rules implement the path /admin/web/...) we can only give you a starting point. You will have to make your way from there. For which you undoubtedly need to take a look into the documentation of the apache http server's mod_rewrite which you want to use on that dynamic configuration file.
Here is something whici I assume implements something along the lines of what you are looking for. You certainly need to adapt it to your needs, though. You'd need to revise your question and be much more precise in what you actually want if you need further help with that. There is an edit button below your question. Use it ...
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/development/
RewriteRule ^ /index.html [R=301]
RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteRule ^/?admin/(.*)$ /admin/web/$1 [END]
RewriteCond %{REQUEST_URI} !^/development/web/
RewriteRule ^/?development/(.*)$ /development/web/$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to rewrite a lot of urls at once in .htacces file? [duplicate]

This question already has an answer here:
How to get query string and rewrite it in htaccess
(1 answer)
Closed 4 years ago.
I am moving my CMS to Wordpress and i will redirect all my old urls using .htaccess file.
I tried a lot of things, but it wil not work
Does anyone know how I can convert all the urls using one simple code?
(I am not moving my domain name, so the domainname will still be the same)
My old urls:
http://mywebsite.com/article_read.php/?a=7148
http://mywebsite.com/article_read.php/?a=4245
http://mywebsite.com/article_read.php/?a=7741
and so on...
My new urls:
http://mywebsite.com/wordpress/?p=7148
http://mywebsite.com/wordpress/?p=4245
http://mywebsite.com/wordpress/?p=7741
and so on...
(Please notice the "a" in the old url, and the "p" in the new url. That makes it too difficult for me to fix it out)
Thanks,
Oktay
This should work (my original answer, it may be missing a backslash after php and an escape ?)
RewriteCond %{REQUEST_URI} ^/article_read.php?a=([0-9]+)$
RewriteRule ^article_read.php?a=([0-9]+)$ /wordpress/?p=$1 [L]
My answer edited by someone else. I'll put both here for reference
RewriteCond %{REQUEST_URI} ^/article_read.php\?a=([0-9]+)$
RewriteRule ^article_read.php$ /wordpress/?p=%1 [L]
This should roughly be what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^|&a=(\d+)$|&
RewriteRule ^/?article_read\.php/?$ /wordpress/?p=%1 [R=301]
That rule should work likewise in the http servers host configuration or in a dynamic configuration file (".htaccess" style file). If you decide to use such a dynamic file then the interpretation of such files needs to be enabled inside the http servers host configuration and you need to place the file in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
The code you give is redirecting my urls to:
http://mysebsite.com/wordpress/
Even when i directly add the code in de Apache and Nginx config. So redirection works, but not to the right url.
#Mahtheww Page
The code you give does nothing (both of them). It does not redirect. Even not to a wrong url.

WAMP - Unwanted prefix https:// on localhost url even though not having any htaccess on root folder

I have some projects on my wamp/www/ directory. Some of my projects, when accessed through 127.0.0.1/project_name or localhost/project_name automatically adds an https:// prefix on the url so it becomes https://127.0.0.1/project_name, which then causes the site to be unreachable.
These projects DOESN'T have any .htaccess files on their root directories. While this may be true to some of my projects, some of my projects are ok and doesn't add unwanted https:// prefix on url.
I'm using WAMP 3.0.6
One more thing. Make sure you remove this from you head section if you have it. It forces the resources (jpg, css, js) to load with https.
<meta http-equiv="Content-Security-Policy"content="upgrade-insecure-requests">
Apache web servers are able to rewrite/redirect incoming requests/urls. Globally this behavior can be set using the httpd.conf or php.ini files for example.
Typically when the default or global behavior needs to be modified for a certain directory, an .htaccess file can be used to define different behavior for requests to that directory/project.
Look in the root of the projects that are being redirected as https:// for an .htaccess file . If you see the lines below in the .htaccess file, you can remove them or modify them to ensure the server handles requests as you expect.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

URL rewriting with parameters in .htaccess

I've been searching lot of related tutorials and so on from Google to solve this on my own, but with zero luck. Therefore I am here to ask. I am trying to 'prettify' my project URL by rewriting. I am not sure are these all achievable anyhow, because I am just starting to get my head around the subject.
I am working 'example' on localhost project folder localhost/example. File '.htaccess' is located in that folder. Where I have set the following:
RewriteEngine On
RewriteBase /example
So basically my application now generates a URL consisting at least 1 parameter all the time and another pointing current location.
Current URL: localhost/example/admin.php?e=2&p=frontpage
Fantasy: localhost/example/admin/2/frontpage
About the parameters:
p stands for selected page
e stands for event
Okay lets think this all is achievable easily, do I have to change all the attributes to match current shown url?
Now they are:
href="?e=2&p=settings"
Should they be:
href="2/settings" ?
I am checking what value GET parameter P has, then including that page into content area.
That is pretty much it, pretty too complex for me, but for education purposes I really want to understand this thru and thru. Thank you.
EDIT:
With the added
RewriteRule ^admin.php/(.*)$ /admin.php?e=$1 [L,QSA]
I am getting lot of pathing errors, whole site is without styling and js files.
EDIT 2:
RewriteEngine On
RewriteBase /example
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /admin.php/e=?(.*)$/p=?(.*)$ /admin.php?e=$1?p=$2 [L,QSA]
Now urls are following:
http://localhost/example/admin.php/2/inc/vex/vex.css
http://localhost/example/admin.php/2/css/modestgrid.css
It is not showing the page in url and the paths are not correct.
They should be http://localhost/example/admin.php/css/modestgrid.css
Your question is a bit vague, contradictory and it is unclear how you actually want to handle (reference) your asset files. But in general I'd say this should be a starting point to get you going:
RewriteEngine On
RewriteBase /example
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.*)$ $1.php?e=$2&p=$3 [END]
For this to work you obviously need the apache rewriting module to be installed and loaded, you need to take care that the interpretation of dynamic configuration files is enabled at all (AllowOverride directive) and you have to place such file in the correct location with reading permission for the http server process.
In case you get an internal server error (http status 500) for that chances are that you operate a very old version of the apache http server. In that case you probably need to replace the [END] flag with the [L] flag which probably will work here too. You will find a hint on that in your http servers error log file in that case.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

htaccess "faking subdomains"

I've been looking at htaccess and how to use rewrite rules in order to make a sort of "fake subdomain" for users.
so far i have:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.domain.com [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.domain\.com$
RewriteRule ^$ index.php?user=%1
this will take any /somthing and make it a subdomain and then pass the subdomain value in as a param to pick up.
i want to be able to now use actual parameters, and pass them through normaly with the extra "user" param from the "subdomain"
e.g.
fred.domain.com/index.php?page=1&sort=up
would give me in $_GET
['user'] = 'fred'
['page']= 1
['sort'] = up
but for the life of me I cant figure out how to do this! as when i add any other params, I loose the user bit
Any help? =)
Also any helpful tutorials on htaccess would be nice! as all ones ive found haven't really explained what each bit does and why =\
Thanks in advance
.htaccess file has nothing to do here. It can't help you.
To direct a user to your server, you have to alter DNS record, not web-server config.
I am assuming that:
You own a domain, domain.com say, that is being hosted by some shared hosting service (SHS) provider.
You either have access to a control panel to set up subdomains, or your SHS provider has already set up a * A record mapping to its shared service.
Some SHS providers simply map *.domain.com to a fixed subdirectory, say /webroot/domain.com/public_html; others do this top level-redirection for their users and provide a control panel which allow account-holders to associate subdomains with specific subdirectories.
So you need to work out which applies in your case:
by looking at your SHS providers FAQs,
by trying http://sss.domain.com/ to see if it routes to your DOCROOT
by using a phpinfo() script to see what Rewrite environment variable it uses to point to your DOCROOT. (For technical reasons in Apache, see Dynamic mass virtual hosts with mod_rewrite, %{DOCUMENT_ROOT} cannot be properly initialised for each user, so providers typically use a RewriteMap to initialise a stand-in; mine uses %{ENV:DOCUMENT_ROOT_REAL})
So let's assume that you want to set up a blog at say http://blog.domain.com/, you may need to:
Issue permanent redirects [R=301] from legacy references to http://domain.com/blog/* to http://blog.domain.com/*
Issue internal redirects from http://blog.domain.com/* to DOCROOT/blogdir/*
Add the necessary conditional interlocks to prevent infinite redirection loops, where this second internal redirect is then treated as a legacy reference.
I could give you a set of rewrite rules to do this, but given that I've answered a dozen flavours of this same Q this months, you can find lots of templates by searching or by looking at my blog (Webfusion, .htaccess) where I've written a number of articles giving more explanation.
If you want a single application to catch some wildcard subset of domain, as long as you can encode this in a regexp then you can do something like:
RewriteCond %{HTTP_HOST} (sompattern)\.domain\.com
RewriteRule ^(?!appdir/).* appdir/catchall.php?arg=$0&subdomain=%1 [L,QSA]
The (?!appdir/) bit is called a lookahead negative assertion and this stops the rule refiring on itself.
I'm pretty sure you can set vhost *.yourdomain.com to point to a single path and then use mod_rewrite for the url parsing.

Categories